Guest User

Untitled

a guest
Feb 15th, 2021
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.74 KB | None | 0 0
  1. use std::ops::Deref;
  2.  
  3. struct Big<'a> {
  4.    option: BigOption,
  5.    small: Small<&'a SmallOption>,
  6. }
  7.  
  8. struct Small<T: Deref<Target = SmallOption>> {
  9.     option: T,
  10. }
  11.  
  12. struct BigOption {
  13.     a: u32,
  14.     small_option: SmallOption,
  15. }
  16.  
  17. struct SmallOption {
  18.     b: u32,
  19. }
  20.  
  21. impl<T: Deref<Target = SmallOption>> Small<T> {
  22.     fn new(option: T) -> Self {
  23.         Self {
  24.             option,
  25.         }
  26.     }
  27. }
  28.  
  29. impl<'a> Big<'a> {
  30.     fn new(option: BigOption) -> Self {
  31.         let small = Small::new(&option.small_option);
  32.         Self { option, small }
  33.     }
  34. }
  35.  
  36. fn main() {
  37.     let big_option = BigOption {
  38.         a: 123,
  39.         small_option: SmallOption {
  40.             b: 456,
  41.         }
  42.     };
  43.     Big::new(big_option);
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment