What does “expected value, found trait” mean?

▼魔方 西西 提交于 2020-01-07 04:25:15

问题


I am trying to build a scene manager that lets you push scenes onto a stack. When each scene is popped off the stack, it is run until stopped and then we repeat.

An example is a menu in a game; which is one scene. When you close it, the game map behind it is another scene.

pub trait Scene {
    fn start(&mut self) {}
    fn update(&mut self) {}
    fn stop(&mut self) {}
    fn is_active(&self) -> bool {
        return false;
    }
}

pub struct SceneManager {
    scenes: Vec<Box<Scene>>,
}

impl SceneManager {
    fn new<T>(scene: T) -> SceneManager
    where
        T: Scene + 'static,
    {
        SceneManager { scenes: vec![Box::new(scene)] }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    struct Sample {
        running: bool,
    }

    impl Scene for Sample {
        fn start(&mut self) {
            self.running = true;
        }

        fn update(&mut self) {
            if self.running {
                self.stop()
            }
        }

        fn stop(&mut self) {
            self.running = false;
        }

        fn is_active(&self) -> bool {
            self.running
        }
    }

    #[test]
    fn test_is_running() {
        let scene_manager = SceneManager::new(Scene);
    }
}

The Scene trait is implemented for some structure that contains some way to tell if that scene is running or not. In this case, a structure called Sample.

You implement the Scene for Sample and then push that scene to the scene manager).

error[E0423]: expected value, found trait `Scene`
  --> src/engine/scene.rs:48:47
   |
48 |         let scene_manager = SceneManager::new(Scene);
   |                                               ^^^^^ not a value

Not exactly sure what to do at this point. How do I get my scene on to the "stack" of scenes? I implemented the new function of SceneManager to take a type where the type matches a Scene definition (if I understood that correctly). This alleviates me of having to specify a specific size and thus allowing me to push it to the heap instead of the stack.

What am I doing wrong and how do I alleviate the problem at hand and what does this even mean?


回答1:


Here Scene is the name for a trait, but SceneManager::new accepts a value of type Scene. You will probably want to do this

let scene_manager = SceneManager::new(Sample { running: false }); 


来源:https://stackoverflow.com/questions/45581356/what-does-expected-value-found-trait-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!