SHARE
TWEET

Untitled

a guest Aug 24th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #[derive(Debug)]
  2. struct Index {
  3.     inner: Vec<Description>
  4. }
  5.  
  6. impl Index {
  7.     fn insert(&mut self, element: impl Described) {
  8.        let foo = element.foo();
  9.        let bar = element.bar();
  10.        self.inner.push(Description { foo, bar });
  11.     }
  12. }
  13.  
  14. #[derive(Debug)]
  15. struct Description {
  16.     foo: String,
  17.     bar: String,
  18. }
  19.  
  20. trait Described {
  21.     fn foo(&self) -> String;
  22.     fn bar(&self) -> String;
  23. }
  24.  
  25. impl<'a, T> From<&'a [T]> for Index
  26. where &'a T: Described
  27. {
  28.     fn from(vec: &'a [T]) -> Index {
  29.         let mut index = Index { inner: Vec::new() };
  30.         for element in vec {
  31.             index.insert(element);
  32.         }
  33.         index
  34.     }
  35. }
  36.  
  37. struct Other(String, String);
  38.  
  39. impl Other {
  40.     fn new(foo: &str, bar: &str) -> Self {
  41.         Other(foo.to_owned(), bar.to_owned())
  42.     }
  43. }
  44.  
  45. impl Described for &Other {
  46.     fn foo(&self) -> String {
  47.         self.0.clone()
  48.     }
  49.    
  50.     fn bar(&self) -> String {
  51.         self.1.clone()
  52.     }
  53. }
  54.  
  55. fn main() {
  56.     let others = vec![
  57.         Other::new("a", "b"),
  58.         Other::new("c", "d"),
  59.     ];
  60.     let index = Index::from(others.as_slice());
  61.     println!("{:#?}", index);
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top