daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Dec 16th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Sample {
  2.     pub v: Vec<u8>, pub c: usize,
  3. }
  4.  
  5. fn main() {
  6.     let mut v=Sample{v: vec![0; 16],c: 2};
  7.     pixel(&mut v, 1)[0]=-1i16;
  8.     pixel(&mut v, 3)[0]=1i16;
  9.     pixel(&mut v, 3)[1]=-1i16;
  10.     assert_eq!(v.v,vec![0,0,0,0,255,255,0,0,0,0,0,0,1,0,255,255])
  11. }
  12.  
  13. fn pixel<'a>(buf: &'a mut Sample, pos: usize) -> &'a mut [i16] {
  14.     let win=2*buf.c;
  15.     let pos=pos*win;
  16.     assert!(pos>=0 && pos+win<=buf.v.len());
  17.     let sl: &mut [u8] = &mut buf.v[pos..pos+win];
  18.     let slp=sl.as_mut_ptr() as *mut i16;
  19.     unsafe {std::slice::from_raw_parts_mut(slp,buf.c)}
  20. }
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