Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::path::Path;
- enum Storage<'d> {
- Borrowed(&'d [u8]),
- Owned(String),
- }
- pub struct Main<'d> {
- storage: Storage<'d>,
- //bytes: Option<&[u8]> // CAN'T DO THIS!
- }
- impl<'d> Main<'d> {
- pub fn new(binary_data: &[u8]) -> Main {
- // This one uses Borrowed
- Main { storage: Storage::Borrowed(binary_data) }
- }
- pub fn from_file<P: AsRef<Path>>(_file_path: P) -> Main<'static> {
- // This one uses Owned
- Main { storage: Storage::Owned("testdata".to_string()) }
- }
- pub fn get_bytes(&mut self) -> &[u8] {
- let bytes = match self.storage {
- Storage::Borrowed(b) => { b }
- Storage::Owned(ref o) => { o.as_bytes() }
- };
- //self.bytes = Some(bytes) // CAN'T DO THIS!
- bytes
- }
- }
- fn main() {
- let mut m = Main::from_file("foobar");
- let b = m.get_bytes();
- println!("{:?}", b);
- let b = m.get_bytes();
- println!("{:?}", b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement