Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. use std::path::Path;
  2.  
  3. enum Storage<'d> {
  4. Borrowed(&'d [u8]),
  5. Owned(String),
  6. }
  7.  
  8. pub struct Main<'d> {
  9. storage: Storage<'d>,
  10. //bytes: Option<&[u8]> // CAN'T DO THIS!
  11. }
  12.  
  13. impl<'d> Main<'d> {
  14. pub fn new(binary_data: &[u8]) -> Main {
  15. // This one uses Borrowed
  16. Main { storage: Storage::Borrowed(binary_data) }
  17. }
  18.  
  19. pub fn from_file<P: AsRef<Path>>(_file_path: P) -> Main<'static> {
  20. // This one uses Owned
  21. Main { storage: Storage::Owned("testdata".to_string()) }
  22. }
  23.  
  24. pub fn get_bytes(&mut self) -> &[u8] {
  25. let bytes = match self.storage {
  26. Storage::Borrowed(b) => { b }
  27. Storage::Owned(ref o) => { o.as_bytes() }
  28. };
  29.  
  30. //self.bytes = Some(bytes) // CAN'T DO THIS!
  31.  
  32. bytes
  33. }
  34. }
  35.  
  36. fn main() {
  37. let mut m = Main::from_file("foobar");
  38.  
  39. let b = m.get_bytes();
  40.  
  41. println!("{:?}", b);
  42.  
  43. let b = m.get_bytes();
  44.  
  45. println!("{:?}", b);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement