Test to understand how to refer to things sharing a lifetime.

This commit is contained in:
Bill Thiede 2023-02-15 20:57:03 -08:00
commit 5355b4388e
4 changed files with 49 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View 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
View 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
View 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();
}