Test to understand how to refer to things sharing a lifetime.
This commit is contained in:
commit
5355b4388e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustrefs"
|
||||||
|
version = "0.1.0"
|
||||||
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "rustrefs"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
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();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user