fork download
  1. use std::collections::BTreeMap;
  2. use std::convert::{From, Into};
  3. use std::borrow::ToOwned;
  4.  
  5. #[derive(Debug)]
  6. enum Foo {
  7. Bytes(Vec<u8>),
  8. String(String),
  9. I32(i32),
  10. I64(i64),
  11. Dict(Dict),
  12. }
  13.  
  14. impl<'a> From<&'a [u8]> for Foo {
  15. fn from(b: &'a [u8]) -> Self { Foo::Bytes(b.to_owned()) }
  16. }
  17.  
  18. impl<'a> From<&'a str> for Foo {
  19. fn from(s: &'a str) -> Self { Foo::String(s.to_owned()) }
  20. }
  21.  
  22. impl From<i32> for Foo {
  23. fn from(n: i32) -> Self { Foo::I32(n) }
  24. }
  25.  
  26. impl From<i64> for Foo {
  27. fn from(n: i64) -> Self { Foo::I64(n) }
  28. }
  29.  
  30. impl From<Dict> for Foo {
  31. fn from(d: Dict) -> Self { Foo::Dict(d) }
  32. }
  33.  
  34. #[derive(Debug)]
  35. pub struct Dict {
  36. data: BTreeMap<Vec<u8>, Foo>
  37. }
  38.  
  39. impl Dict {
  40. pub fn push<K, V>(&mut self, key: K, val: V)
  41. where K: Into<Vec<u8>>,
  42. V: Into<Foo> {
  43. self.data.insert(key.into(), val.into());
  44. }
  45. }
  46.  
  47. fn main() {}
Success #stdin #stdout 0s 10992KB
stdin
Standard input is empty
stdout
Standard output is empty