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 all Cows #473

Merged
merged 1 commit into from
May 15, 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
4 changes: 4 additions & 0 deletions firmware/qemu/src/bin/alloc.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
0.000008 INFO Vec<Box<i32>>: [-1, 2, 3, 4]
0.000009 INFO Box<Vec<i32>>: [-1, 2, 3, 4]
0.000010 INFO String: Hello! I'm a heap-allocated String
0.000011 INFO Cow<[u32]>: [1, 2, 3, 4]
0.000012 INFO Cow<Vec<u32>>: [1, 2, 3, 4]
0.000013 INFO Cow<str>: moo
0.000014 INFO Cow<String>: moo, but allocated
4 changes: 4 additions & 0 deletions firmware/qemu/src/bin/alloc.release.out
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
0.000008 INFO Vec<Box<i32>>: [-1, 2, 3, 4]
0.000009 INFO Box<Vec<i32>>: [-1, 2, 3, 4]
0.000010 INFO String: Hello! I'm a heap-allocated String
0.000011 INFO Cow<[u32]>: [1, 2, 3, 4]
0.000012 INFO Cow<Vec<u32>>: [1, 2, 3, 4]
0.000013 INFO Cow<str>: moo
0.000014 INFO Cow<String>: moo, but allocated
5 changes: 5 additions & 0 deletions firmware/qemu/src/bin/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

extern crate alloc;

use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::String;
Expand Down Expand Up @@ -37,6 +38,10 @@ fn main() -> ! {
"String: {=?}",
String::from("Hello! I'm a heap-allocated String")
);
defmt::info!("Cow<[u32]>: {=?}", Cow::from(&[1u32, 2, 3, 4][..]));
defmt::info!("Cow<Vec<u32>>: {=?}", Cow::from(vec![1u32, 2, 3, 4]));
defmt::info!("Cow<str>: {=?}", Cow::from("moo"));
defmt::info!("Cow<String>: {=?}", String::from("moo, but allocated"));

loop {
debug::exit(debug::EXIT_SUCCESS)
Expand Down
16 changes: 16 additions & 0 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,22 @@ mod if_alloc {
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)
}
}
}

impl Format for core::convert::Infallible {
Expand Down