Test to understand how to refer to things sharing a lifetime.
This commit is contained in:
33
src/main.rs
Normal file
33
src/main.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
trait Material {}
|
||||
struct Impl1 {}
|
||||
impl Material for Impl1 {}
|
||||
|
||||
trait Shape {}
|
||||
struct Impl2<M: Material> {
|
||||
v: M,
|
||||
}
|
||||
impl<M> Shape for Impl2<M> where M: Material {}
|
||||
impl Material for &Box<dyn Material> {}
|
||||
|
||||
struct State {
|
||||
materials: HashMap<String, Box<dyn Material>>,
|
||||
shape: Box<dyn Shape>,
|
||||
}
|
||||
|
||||
fn load_map() -> State {
|
||||
let mut materials: HashMap<String, Box<dyn Material>> = HashMap::new();
|
||||
materials.insert("one".to_string(), Box::new(Impl1 {}));
|
||||
materials.insert("two".to_string(), Box::new(Impl1 {}));
|
||||
|
||||
let shape = Box::new(Impl2 {
|
||||
v: materials.get("one").unwrap(),
|
||||
});
|
||||
|
||||
State { materials, shape }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = load_map();
|
||||
}
|
||||
Reference in New Issue
Block a user