Skip to content

Commit

Permalink
Merge #473
Browse files Browse the repository at this point in the history
473: Impl `Format` for all `Cow`s r=Urhengulas a=mattico



Co-authored-by: Matt Ickstadt <mattico8@gmail.com>
  • Loading branch information
bors[bot] and mattico authored May 15, 2021
2 parents f6270ae + 1f86403 commit 0da0e51
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
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

0 comments on commit 0da0e51

Please sign in to comment.