Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- use std::hash::Hash;
- use std::boxed::Box;
- struct Super<'a> {
- a: A<'a, String>
- }
- impl<'a> Super<'a> {
- fn new() -> Self {
- let mut a = A::<'a, String>::new();
- let b = B::<'a>::new("hello");
- a.insert("first".to_string(), b);
- Self {
- a: a
- }
- }
- fn count(&self) {
- println!("{}", self.a.children.len())
- }
- }
- trait Trait {}
- struct B<'a> {
- desc: &'a str,
- }
- impl Trait for B<'_> {}
- impl<'a> B<'a> {
- fn new(desc: &'a str) -> Self {
- B::<'a> {
- desc: desc
- }
- }
- }
- struct A<'a, K>
- where K: Hash + Eq
- {
- children: HashMap<K, Box<dyn Trait + 'a>>
- }
- impl<'a, K> A<'a, K>
- where K: Hash + Eq
- {
- fn new() -> Self {
- Self {
- children: HashMap::new(),
- }
- }
- fn insert<T>(&mut self, key: K, value: T)
- where T: Trait, T: 'a
- {
- self.children.insert(key, Box::new(value));
- }
- }
- fn main() {
- let s = Super::new();
- s.count();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement