Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. use std::collections::HashMap;
  2. use std::hash::Hash;
  3. use std::boxed::Box;
  4.  
  5. struct Super<'a> {
  6. a: A<'a, String>
  7. }
  8.  
  9. impl<'a> Super<'a> {
  10. fn new() -> Self {
  11. let mut a = A::<'a, String>::new();
  12. let b = B::<'a>::new("hello");
  13. a.insert("first".to_string(), b);
  14.  
  15. Self {
  16. a: a
  17. }
  18. }
  19.  
  20. fn count(&self) {
  21. println!("{}", self.a.children.len())
  22. }
  23. }
  24.  
  25. trait Trait {}
  26.  
  27. struct B<'a> {
  28. desc: &'a str,
  29. }
  30.  
  31. impl Trait for B<'_> {}
  32.  
  33. impl<'a> B<'a> {
  34. fn new(desc: &'a str) -> Self {
  35. B::<'a> {
  36. desc: desc
  37. }
  38. }
  39. }
  40.  
  41. struct A<'a, K>
  42. where K: Hash + Eq
  43. {
  44. children: HashMap<K, Box<dyn Trait + 'a>>
  45. }
  46.  
  47. impl<'a, K> A<'a, K>
  48. where K: Hash + Eq
  49. {
  50. fn new() -> Self {
  51. Self {
  52. children: HashMap::new(),
  53. }
  54. }
  55.  
  56. fn insert<T>(&mut self, key: K, value: T)
  57. where T: Trait, T: 'a
  58. {
  59. self.children.insert(key, Box::new(value));
  60. }
  61. }
  62.  
  63. fn main() {
  64. let s = Super::new();
  65. s.count();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement