Skip to content

Commit

Permalink
Structure impl Formats into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
Urhengulas committed May 27, 2021
1 parent c8291f4 commit fff9e1b
Show file tree
Hide file tree
Showing 9 changed files with 617 additions and 610 deletions.
610 changes: 0 additions & 610 deletions src/impls.rs

This file was deleted.

60 changes: 60 additions & 0 deletions src/impls/alloc_.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use super::*;

impl<T> Format for alloc::boxed::Box<T>
where
T: ?Sized + Format,
{
fn format(&self, f: Formatter) {
T::format(&*self, f)
}
}

impl<T> Format for alloc::rc::Rc<T>
where
T: ?Sized + Format,
{
fn format(&self, f: Formatter) {
T::format(&*self, f)
}
}

#[cfg(not(no_cas))]
impl<T> Format for alloc::sync::Arc<T>
where
T: ?Sized + Format,
{
fn format(&self, f: Formatter) {
T::format(&*self, f)
}
}

impl<T> Format for alloc::vec::Vec<T>
where
T: Format,
{
fn format(&self, f: Formatter) {
self.as_slice().format(f)
}
}

impl Format for alloc::string::String {
fn format(&self, f: Formatter) {
self.as_str().format(f)
}
}

impl<'a, T> Format for alloc::borrow::Cow<'a, [T]>
where
T: 'a + Format,
[T]: alloc::borrow::ToOwned<Owned = alloc::vec::Vec<T>>,
{
fn format(&self, f: Formatter) {
self.as_ref().format(f)
}
}

impl<'a> Format for alloc::borrow::Cow<'a, str> {
fn format(&self, f: Formatter) {
self.as_ref().format(f)
}
}
87 changes: 87 additions & 0 deletions src/impls/arrays.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use super::*;

macro_rules! arrays {
( $($len:literal $fmt:literal,)+ ) => { $(
impl<T> Format for [T; $len]
where
T: Format
{
fn format(&self, fmt: Formatter) {
if fmt.inner.needs_tag() {
let t = internp!($fmt);
fmt.inner.u8(&t);
}
fmt.inner.fmt_array(self);
}
}
)+ };
}

arrays! {
0 "{=[?;0]}",
1 "{=[?;1]}",
2 "{=[?;2]}",
3 "{=[?;3]}",
4 "{=[?;4]}",
5 "{=[?;5]}",
6 "{=[?;6]}",
7 "{=[?;7]}",
8 "{=[?;8]}",
9 "{=[?;9]}",
10 "{=[?;10]}",
11 "{=[?;11]}",
12 "{=[?;12]}",
13 "{=[?;13]}",
14 "{=[?;14]}",
15 "{=[?;15]}",
16 "{=[?;16]}",
17 "{=[?;17]}",
18 "{=[?;18]}",
19 "{=[?;19]}",
20 "{=[?;20]}",
21 "{=[?;21]}",
22 "{=[?;22]}",
23 "{=[?;23]}",
24 "{=[?;24]}",
25 "{=[?;25]}",
26 "{=[?;26]}",
27 "{=[?;27]}",
28 "{=[?;28]}",
29 "{=[?;29]}",
30 "{=[?;30]}",
31 "{=[?;31]}",
32 "{=[?;32]}",
64 "{=[?;64]}",
128 "{=[?;128]}",
256 "{=[?;256]}",
512 "{=[?;512]}",
1024 "{=[?;1024]}",
2048 "{=[?;2048]}",
4096 "{=[?;4096]}",
8192 "{=[?;8192]}",
16384 "{=[?;16384]}",
32768 "{=[?;32768]}",
65536 "{=[?;65536]}",
131072 "{=[?;131072]}",
262144 "{=[?;262144]}",
524288 "{=[?;524288]}",
1048576 "{=[?;1048576]}",
2097152 "{=[?;2097152]}",
4194304 "{=[?;4194304]}",
8388608 "{=[?;8388608]}",
16777216 "{=[?;16777216]}",
33554432 "{=[?;33554432]}",
67108864 "{=[?;67108864]}",
134217728 "{=[?;134217728]}",
268435456 "{=[?;268435456]}",
536870912 "{=[?;536870912]}",
1073741824 "{=[?;1073741824]}",
100 "{=[?;100]}",
1000 "{=[?;1000]}",
10000 "{=[?;10000]}",
100000 "{=[?;100000]}",
1000000 "{=[?;1000000]}",
10000000 "{=[?;10000000]}",
100000000 "{=[?;100000000]}",
1000000000 "{=[?;1000000000]}",
}
91 changes: 91 additions & 0 deletions src/impls/core_/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//! Some of these objects don't expose enough to accurately report their debug state. In this case
//! we show as much state as we can. Users can always use `Debug2Format` to get more information,
//! at the cost of bringing core::fmt into the firmware and doing the layout work on device.
//!
//! We generally keep the type parameter trait bounds in case it becomes possible to use this
//! later, without making a backwards-incompatible change.

mod ops;
mod slice;

use super::*;

impl<T> Format for Option<T>
where
T: Format,
{
fn format(&self, f: Formatter) {
if f.inner.needs_tag() {
let t = internp!("None|Some({=?})");
f.inner.u8(&t);
}
match self {
None => f.inner.u8(&0),
Some(x) => {
f.inner.u8(&1);
f.inner.with_tag(|f| x.format(f))
}
}
}
}

impl<T, E> Format for Result<T, E>
where
T: Format,
E: Format,
{
fn format(&self, f: Formatter) {
if f.inner.needs_tag() {
let t = internp!("Err({=?})|Ok({=?})");
f.inner.u8(&t);
}
match self {
Err(e) => {
f.inner.u8(&0);
f.inner.with_tag(|f| e.format(f))
}
Ok(x) => {
f.inner.u8(&1);
f.inner.with_tag(|f| x.format(f))
}
}
}
}

impl<T> Format for core::marker::PhantomData<T> {
fn format(&self, f: Formatter) {
if f.inner.needs_tag() {
let t = internp!("PhantomData");
f.inner.u8(&t);
}
}
}

impl Format for core::convert::Infallible {
#[inline]
fn format(&self, _: Formatter) {
// type cannot be instantiated so nothing to do here
match *self {}
}
}

impl Format for core::time::Duration {
fn format(&self, fmt: Formatter) {
crate::write!(
fmt,
"Duration {{ secs: {=u64}, nanos: {=u32} }}",
self.as_secs(),
self.subsec_nanos(),
)
}
}

impl<A, B> Format for core::iter::Zip<A, B>
where
A: Format,
B: Format,
{
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "Zip(..)")
}
}
52 changes: 52 additions & 0 deletions src/impls/core_/ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::*;

impl<Idx> Format for core::ops::Range<Idx>
where
Idx: Format,
{
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "{}..{}", self.start, self.end)
}
}

impl<Idx> Format for core::ops::RangeFrom<Idx>
where
Idx: Format,
{
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "{}..", self.start)
}
}

impl Format for core::ops::RangeFull {
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "..",)
}
}

impl<Idx> Format for core::ops::RangeInclusive<Idx>
where
Idx: Format,
{
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "{}..={}", self.start(), self.end())
}
}

impl<Idx> Format for core::ops::RangeTo<Idx>
where
Idx: Format,
{
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "..{}", self.end)
}
}

impl<Idx> Format for core::ops::RangeToInclusive<Idx>
where
Idx: Format,
{
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "..={}", self.end)
}
}
32 changes: 32 additions & 0 deletions src/impls/core_/slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::*;

impl<'a, T: 'a> Format for core::slice::ChunksExact<'a, T>
where
T: Format,
{
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "ChunksExact(..)")
}
}

impl<'a, T: 'a> Format for core::slice::Iter<'a, T>
where
T: Format,
{
fn format(&self, fmt: Formatter) {
crate::write!(
fmt,
"Iter {{ slice: {=[?]}, position: ? }}",
self.as_slice()
)
}
}

impl<'a, T: 'a> Format for core::slice::Windows<'a, T>
where
T: Format,
{
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "Windows(..)")
}
}
10 changes: 10 additions & 0 deletions src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[cfg(feature = "alloc")]
mod alloc_;
mod arrays;
mod core_;
mod primitives;
mod tuples;

use defmt_macros::internp;

use crate::{self as defmt, Format, Formatter, Str};
Loading

0 comments on commit fff9e1b

Please sign in to comment.