diff --git a/examples/example3.rs b/examples/example3.rs index b57056e826f83a5610bba35b54a5739c8e9d85fe..55147b8332da363fb8c1c53575b5b41e858eae82 100644 --- a/examples/example3.rs +++ b/examples/example3.rs @@ -1,7 +1,7 @@ extern crate array_debug; use array_debug::ArrayDebug; -use core::ops::{Deref, DerefMut}; +use core::ops::Deref; #[derive(Debug)] #[repr(C)] @@ -19,48 +19,61 @@ struct RegisterBlockNative { } fn main() { - let mut r = RegisterBlockDebug { + let mut rd = RegisterBlockDebug { primitive: 7, small: ArrayDebug::new([1, 3, 4]), big: ArrayDebug::new([8; 44]), }; - r.small[1] = 7; - println!("r :{:?}", r); + rd.small[1] = 7; + println!("rd :{:?}", rd); // auto deref coersion of ArrayDebug - let t: &[u32] = &r.small; + let t: &[u32] = &rd.big; + print!("rd.big ["); for i in t { - println!("{}", i); + print!("{}, ", i); } + println!("]"); + + // // deref goes too far, [u32] is not an iterator + // for i in *rd.small { + // println!("{}", i); + // } + + // `&array_debug::ArrayDebug<[u32; 3], u32>` is not an iterator + // for i in &rd.small { + // println!("{}", i); + // } - // explicit deref required - for i in *r.small { + for i in &*rd.small { println!("{}", i); } - // explicit deref required - for i in r.small.deref() { + // explicit deref works + for i in rd.small.deref() { println!("{}", i); } - - for i in r.small.iter() { - println!("{}", i); + // works, and is consistent with the native type + print!("rd.small ["); + for i in rd.small.iter() { + print!("{}", i); } + println!("]"); - let r = RegisterBlockNative { + let rn = RegisterBlockNative { primitive: 7, small: [1, 3, 4], big: [8; 44], }; - for i in &r.small { + for i in &rn.small { println!("{}", i); } - for i in r.small.iter() { + for i in rn.small.iter() { println!("{}", i); } diff --git a/src/lib.rs b/src/lib.rs index ee2ef831f27f8a86d9ec53249e19645ebf72ae8a..dbaf78ca4e41700f40c7f85d9823f74d192f65a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ where { type Target = [T]; - fn deref(&self) -> &Self::Target { + fn deref(&self) -> &[T] { &self.0 } } @@ -36,7 +36,7 @@ where A: Unsize<[T]>, T: fmt::Debug, { - fn deref_mut(&mut self) -> &mut Self::Target { + fn deref_mut(&mut self) -> &mut [T] { &mut self.0 } }