From 4f850f5211046a83f9db23aa1df3e83992b2ac4e Mon Sep 17 00:00:00 2001 From: Per <Per Lindgren> Date: Thu, 13 Dec 2018 14:32:43 +0100 Subject: [PATCH] more examples --- examples/example3.rs | 45 ++++++++++++++++++++++++++++---------------- src/lib.rs | 4 ++-- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/examples/example3.rs b/examples/example3.rs index b57056e..55147b8 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 ee2ef83..dbaf78c 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 } } -- GitLab