Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Format for NonZero* #509

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion firmware/qemu/src/bin/log.out
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,16 @@
0.000124 INFO ChunksExact(..)
0.000125 INFO Iter { slice: [0, 1, 2], position: ? }
0.000126 INFO Windows(..)
0.000127 INFO QEMU test finished!
0.000127 INFO 1
0.000128 INFO 1
0.000129 INFO 1
0.000130 INFO 1
0.000131 INFO 1
0.000132 INFO 1
0.000133 INFO 1
0.000134 INFO 1
0.000135 INFO 1
0.000136 INFO 1
0.000137 INFO 1
0.000138 INFO 1
0.000139 INFO QEMU test finished!
14 changes: 13 additions & 1 deletion firmware/qemu/src/bin/log.release.out
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,16 @@
0.000122 INFO ChunksExact(..)
0.000123 INFO Iter { slice: [0, 1, 2], position: ? }
0.000124 INFO Windows(..)
0.000125 INFO QEMU test finished!
0.000125 INFO 1
0.000126 INFO 1
0.000127 INFO 1
0.000128 INFO 1
0.000129 INFO 1
0.000130 INFO 1
0.000131 INFO 1
0.000132 INFO 1
0.000133 INFO 1
0.000134 INFO 1
0.000135 INFO 1
0.000136 INFO 1
0.000137 INFO QEMU test finished!
15 changes: 15 additions & 0 deletions firmware/qemu/src/bin/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use core::{
marker::PhantomData,
num,
sync::atomic::{AtomicU32, Ordering},
};
use cortex_m_rt::entry;
Expand Down Expand Up @@ -617,6 +618,20 @@ fn main() -> ! {
defmt::info!("{:?}", [0, 1, 2].iter()); // ChunksExact
defmt::info!("{:?}", [0, 1, 2].windows(1)); // Windows

// core::num::NonZero*
defmt::info!("{}", num::NonZeroI8::new(1).unwrap());
defmt::info!("{}", num::NonZeroI16::new(1).unwrap());
defmt::info!("{}", num::NonZeroI32::new(1).unwrap());
defmt::info!("{}", num::NonZeroI64::new(1).unwrap());
defmt::info!("{}", num::NonZeroI128::new(1).unwrap());
defmt::info!("{}", num::NonZeroIsize::new(1).unwrap());
defmt::info!("{}", num::NonZeroU8::new(1).unwrap());
defmt::info!("{}", num::NonZeroU16::new(1).unwrap());
defmt::info!("{}", num::NonZeroU32::new(1).unwrap());
defmt::info!("{}", num::NonZeroU64::new(1).unwrap());
defmt::info!("{}", num::NonZeroU128::new(1).unwrap());
defmt::info!("{}", num::NonZeroUsize::new(1).unwrap());

defmt::info!("QEMU test finished!");

loop {
Expand Down
1 change: 1 addition & 0 deletions src/impls/core_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! We generally keep the type parameter trait bounds in case it becomes possible to use this
//! later, without making a backwards-incompatible change.

mod num;
mod ops;
mod slice;

Expand Down
26 changes: 26 additions & 0 deletions src/impls/core_/num.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use core::num;

use super::*;

macro_rules! non_zero {
($type:ty, $hint:literal) => {
impl Format for $type {
fn format(&self, fmt: Formatter) {
crate::write!(fmt, $hint, self.get());
}
}
};
}

non_zero! {num::NonZeroI8, "{=i8}"}
non_zero! {num::NonZeroI16, "{=i16}"}
non_zero! {num::NonZeroI32, "{=i32}"}
non_zero! {num::NonZeroI64, "{=i64}"}
non_zero! {num::NonZeroI128, "{=i128}"}
non_zero! {num::NonZeroIsize, "{=isize}"}
non_zero! {num::NonZeroU8, "{=u8}"}
non_zero! {num::NonZeroU16, "{=u16}"}
non_zero! {num::NonZeroU32, "{=u32}"}
non_zero! {num::NonZeroU64, "{=u64}"}
non_zero! {num::NonZeroU128, "{=u128}"}
non_zero! {num::NonZeroUsize, "{=usize}"}