Commit 4f850f52 authored by Per's avatar Per

more examples

parent da378c2a
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);
}
......
......@@ -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
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment