Skip to content
Snippets Groups Projects
Commit da378c2a authored by Per's avatar Per
Browse files

examples

parent 55f8b119
No related branches found
No related tags found
No related merge requests found
Pipeline #86 failed
...@@ -12,12 +12,40 @@ ...@@ -12,12 +12,40 @@
], ],
"problemMatcher": [ "problemMatcher": [
"$rustc" "$rustc"
] ],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "cargo run --example example",
"command": "cargo run --example example",
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "cargo run --example example2",
"command": "cargo run --example example2",
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
}, },
{ {
"type": "shell", "type": "shell",
"label": "cargo build --example example", "label": "cargo run --example example3",
"command": "cargo build --example example", "command": "cargo run --example example3",
"problemMatcher": [ "problemMatcher": [
"$rustc" "$rustc"
], ],
......
# ArrayDebug
A newtype over array, providing `fmt::Debug` formatting for generic sized arrays.
Requires nightly compiler as relying on `#![feature(unsize)]` (awaiting const generics).
## Usage
``` rust
extern crate array_debug;
use array_debug::ArrayDebug;
#[derive(Debug)]
struct RegisterBlock {
primitive: u32,
small: ArrayDebug<[u32; 3], u32>,
big: ArrayDebug<[u32; 44], u32>,
}
fn main() {
let t = ArrayDebug::new([1, 2, 3]);
println!("<=32 :{:?}", t);
let t = ArrayDebug::new([1; 33]);
println!(">32 :{:?}", t);
let r = RegisterBlock {
primitive: 7,
small: ArrayDebug::new([1, 3, 4]),
big: ArrayDebug::new([8; 44]),
};
println!("r :{:?}", r);
}
```
\ No newline at end of file
...@@ -23,6 +23,4 @@ fn main() { ...@@ -23,6 +23,4 @@ fn main() {
}; };
println!("r :{:?}", r); println!("r :{:?}", r);
println!("here");
} }
extern crate array_debug;
use array_debug::ArrayDebug;
use core::mem::transmute;
#[derive(Debug)]
#[repr(C)]
struct RegisterBlockDebug {
primitive: u32,
small: ArrayDebug<[u32; 3], u32>,
big: ArrayDebug<[u32; 44], u32>,
}
#[repr(C)]
struct RegisterBlockNative {
primitive: u32,
small: [u32; 3],
big: [u32; 44],
}
fn main() {
let r = RegisterBlockNative {
primitive: 7,
small: [1, 3, 4],
big: [8; 44],
};
let r: RegisterBlockDebug = unsafe { transmute::<_, _>(r) };
println!("r :{:?}", r);
}
extern crate array_debug;
use array_debug::ArrayDebug;
use core::ops::{Deref, DerefMut};
#[derive(Debug)]
#[repr(C)]
struct RegisterBlockDebug {
primitive: u32,
small: ArrayDebug<[u32; 3], u32>,
big: ArrayDebug<[u32; 44], u32>,
}
#[repr(C)]
struct RegisterBlockNative {
primitive: u32,
small: [u32; 3],
big: [u32; 44],
}
fn main() {
let mut r = RegisterBlockDebug {
primitive: 7,
small: ArrayDebug::new([1, 3, 4]),
big: ArrayDebug::new([8; 44]),
};
r.small[1] = 7;
println!("r :{:?}", r);
// auto deref coersion of ArrayDebug
let t: &[u32] = &r.small;
for i in t {
println!("{}", i);
}
// explicit deref required
for i in *r.small {
println!("{}", i);
}
// explicit deref required
for i in r.small.deref() {
println!("{}", i);
}
for i in r.small.iter() {
println!("{}", i);
}
let r = RegisterBlockNative {
primitive: 7,
small: [1, 3, 4],
big: [8; 44],
};
for i in &r.small {
println!("{}", i);
}
for i in r.small.iter() {
println!("{}", i);
}
let small = [1, 2, 3];
for i in &small {
println!("{}", i);
}
}
#![feature(unsize)] #![feature(unsize)]
use core::iter::Iterator; use core::iter::Iterator;
use core::ops::{Deref, DerefMut};
use core::{fmt, marker::PhantomData, marker::Unsize}; use core::{fmt, marker::PhantomData, marker::Unsize};
pub struct ArrayDebug<A, T>(A, PhantomData<T>) pub struct ArrayDebug<A, T>(A, PhantomData<T>)
...@@ -18,6 +19,28 @@ where ...@@ -18,6 +19,28 @@ where
} }
} }
impl<A, T> Deref for ArrayDebug<A, T>
where
A: Unsize<[T]>,
T: fmt::Debug,
{
type Target = [T];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<A, T> DerefMut for ArrayDebug<A, T>
where
A: Unsize<[T]>,
T: fmt::Debug,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<A, T> fmt::Debug for ArrayDebug<A, T> impl<A, T> fmt::Debug for ArrayDebug<A, T>
where where
A: Unsize<[T]>, A: Unsize<[T]>,
......
#![feature(unsize)]
use core::iter::Iterator;
use core::{fmt, marker::PhantomData, marker::Unsize};
struct ArrayDebug<A, T>(A, PhantomData<T>)
where
A: Unsize<[T]>,
T: fmt::Debug;
impl<A, T> fmt::Debug for ArrayDebug<A, T>
where
A: Unsize<[T]>,
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let slice: &[T] = &self.0;
let mut slice = slice.iter().peekable();
f.write_fmt(format_args!("["))?;
while let Some(i) = slice.next() {
f.write_fmt(format_args!("{:?}", i))?;
if slice.peek().is_some() {
f.write_fmt(format_args!(", "))?;
}
}
f.write_fmt(format_args!("]"))
}
}
// example
#[derive(Debug)]
struct RegisterBlock {
primitive: u32,
small: ArrayDebug<[u32; 3], u32>,
big: ArrayDebug<[u32; 44], u32>,
}
fn main() {
let t = ArrayDebug([1, 2, 3], PhantomData);
println!("<=32 :{:?}", t);
let t = ArrayDebug([1; 33], PhantomData);
println!(">32 :{:?}", t);
let r = RegisterBlock {
primitive: 7,
small: ArrayDebug([1, 3, 4], PhantomData),
big: ArrayDebug([8; 44], PhantomData),
};
println!("r :{:?}", r);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment