SHARE
TWEET

Untitled

a guest Apr 24th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::marker::PhantomData;
  2.  
  3. #[derive(Debug)]
  4. struct Foo<'a> {
  5.     v: Vec<u8>,
  6.     p: PhantomData<&'a u8>,
  7. }
  8.  
  9. impl<'a> Foo<'a> {
  10.     fn bar(&mut self) -> &'a [u8] {
  11.         let b: &[u8] = &self.v[..];
  12.         let n = b.len();
  13.         let ptr = b.as_ptr();
  14.         let ret: &'a [u8];
  15.         unsafe {
  16.             ret = std::slice::from_raw_parts(ptr, n);
  17.         }
  18.         ret
  19.     }
  20. }
  21.  
  22.  
  23. fn main() {
  24.     let v = vec![1u8, 2, 3, 4, 5, 6, 7, 8];
  25.     let b;
  26.     {
  27.     let mut x = Foo {
  28.         v,
  29.         p: PhantomData,
  30.     };
  31.     b = x.bar();
  32.    
  33. }
  34.    
  35.     println!("{:?}", b);
  36. }
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