daily pastebin goal
49%
SHARE
TWEET

Untitled

a guest May 17th, 2018 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate core;
  2.  
  3. use core::marker::PhantomData;
  4. pub struct Mut<'a>(PhantomData<&'a mut ()>);
  5.  
  6. struct Bucket {
  7.     ptr: *mut u32,
  8.     len: usize,
  9. }
  10.  
  11. struct Ref<M> {
  12.     base: *mut u32,
  13.     index: usize,
  14.     _marker: PhantomData<M>,
  15. }
  16.  
  17. impl Bucket {
  18.     pub fn get_first(&self) -> Ref<Mut> {
  19.         Ref {
  20.             base: self.ptr,
  21.             index: 0,
  22.             _marker: PhantomData,
  23.         }
  24.     }
  25.    
  26.     pub fn get(&self, index: usize) -> Ref<Mut> {
  27.         Ref {
  28.             base: self.ptr,
  29.             index,
  30.             _marker: PhantomData,
  31.         }
  32.     }
  33. }
  34.  
  35. impl<'a> Ref<Mut<'a>> {
  36.     pub fn into_mut(self) -> &'a mut u32 {
  37.         unsafe {
  38.             &mut *self.base.add(self.index)
  39.         }
  40.     }
  41. }
  42.  
  43. fn main() {
  44.     let mut data = vec![1, 2, 3];
  45.     /*
  46.     let r;
  47.     {
  48.         let bucket = Bucket { ptr: data.as_mut_ptr(), len: data.len() };
  49.         r = bucket.get_first(); // does this work? should be compile error
  50.     }
  51.     */
  52.     let bucket = Bucket { ptr: data.as_mut_ptr(), len: data.len() };
  53.     let r1 = bucket.get(0);
  54.     let r11 = bucket.get(0);
  55.     let r2 = bucket.get(1);
  56.     let r3 = bucket.get(2);
  57.     println!("Val1 : {}", r1.into_mut());
  58.     println!("Val2 : {}", r2.into_mut());
  59.     println!("Val3 : {}", r3.into_mut());
  60.     //let d1 =
  61.  
  62.     /*
  63.     let r1;
  64.     {
  65.         let mut v1 = 53u32;
  66.         r1 = Ref { ptr : &mut v1 as *mut u32, _p: PhantomData };
  67.     }
  68.     println!("Val : {}", r1.into_mut());
  69.    
  70.     let mut v2 = 64u32;
  71.     let r2;
  72.     {
  73.         let root = Root { ptr : &mut v2 as *mut u32 };
  74.         r2 = root.as_ref();
  75.     }
  76.     println!("Val : {}", r2.into_mut());
  77.     */
  78. }
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