3

With this code:

use std::io::Result;

pub trait Frame {
    type Sample;
}

pub trait WriteFrames<F: Frame> {
    fn write_frames(&mut self, frames: &[F]) -> Result<usize>;
}

pub trait ReadFrames<F: Frame> {
    fn read_frames(&mut self, frames: &mut [F]) -> Result<usize>;
}

pub trait FrameBuffer: ReadFrames<Self::Frame> + WriteFrames<Self::Frame> {
    type Frame: Frame;
}

I get this error:

   Compiling playground v0.0.1 (/playground)
error[E0391]: cycle detected when computing the supertraits of `FrameBuffer`
  --> src/lib.rs:15:1
   |
15 | pub trait FrameBuffer: ReadFrames<Self::Frame> + WriteFrames<Self::Frame> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: ...which again requires computing the supertraits of `FrameBuffer`, completing the cycle
note: cycle used when collecting item types in top-level module
  --> src/lib.rs:15:1
   |
15 | pub trait FrameBuffer: ReadFrames<Self::Frame> + WriteFrames<Self::Frame> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0391`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

How should I model my code to get around this error?

I want to have a FrameBuffer trait that specified a Frame type, and requires WriteFrames and ReadFrames for that Frame type.

Playground link

pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
user330199
  • 43
  • 3
  • 1
    (Specifically, `ReadFrames<::Frame> + WriteFrames<::Frame>` [seems to compile](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=be9199a71784619716b29a6cd1c8348e).) – Ilmari Karonen Sep 01 '19 at 13:51

0 Answers0