Skip to content
Snippets Groups Projects
Commit 55f8b119 authored by Per Lindgren's avatar Per Lindgren
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #85 canceled
# Generated by Cargo
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
\ No newline at end of file
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "cargo",
"label": "cargo build",
"command": "cargo",
"args": [
"build"
],
"problemMatcher": [
"$rustc"
]
},
{
"type": "shell",
"label": "cargo build --example example",
"command": "cargo build --example example",
"problemMatcher": [
"$rustc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
\ No newline at end of file
[package]
name = "array-debug"
version = "0.1.0"
authors = ["Per Lindgren <per.lindgren@ltu.se>"]
edition = "2018"
[dependencies]
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);
println!("here");
}
#![feature(unsize)]
use core::iter::Iterator;
use core::{fmt, marker::PhantomData, marker::Unsize};
pub struct ArrayDebug<A, T>(A, PhantomData<T>)
where
A: Unsize<[T]>,
T: fmt::Debug;
impl<A, T> ArrayDebug<A, T>
where
A: Unsize<[T]>,
T: fmt::Debug,
{
pub fn new(x: A) -> ArrayDebug<A, T> {
ArrayDebug(x, PhantomData)
}
}
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!("]"))
}
}
#![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