简体   繁体   English

“期望的价值,发现的特征”是什么意思?

[英]What does “expected value, found trait” mean?

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. Scene特征是为某些结构实现的,该结构包含某种方式来指示该场景是否正在运行。 In this case, a structure called Sample . 在这种情况下,称为Sample的结构。

You implement the Scene for Sample and then push that scene to the scene manager). 您实现Scene Sample ,然后将该场景推送到场景管理器)。

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). 我实现了SceneManagernew功能,以采用与Scene定义匹配的类型(如果我正确理解的话)。 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? 我在做错什么,如何减轻眼前的问题,这甚至意味着什么?

Here Scene is the name for a trait, but SceneManager::new accepts a value of type Scene . 这里Scene是特征的名称,但是SceneManager::new接受一个Scene类型的值。 You will probably want to do this 您可能会想要这样做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM