Skip to content

Commit a28dac7

Browse files
bors[bot]ijlkorken89
authored
Merge #143
143: Implement core::fmt::Write for heapless::Vec<u8, N> r=korken89 a=ijl I didn't see a mention of this in issues or previous pull requests, but I expect it's in scope? Co-authored-by: ijl <ijl@mailbox.org> Co-authored-by: Emil Fresk <emil.fresk@gmail.com>
2 parents da15496 + 373abf1 commit a28dac7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/vec.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,18 @@ where
435435
}
436436
}
437437

438+
impl<N> fmt::Write for Vec<u8, N>
439+
where
440+
N: ArrayLength<u8>,
441+
{
442+
fn write_str(&mut self, s: &str) -> fmt::Result {
443+
match self.extend_from_slice(s.as_bytes()) {
444+
Ok(()) => Ok(()),
445+
Err(_) => Err(fmt::Error),
446+
}
447+
}
448+
}
449+
438450
impl<T, N> Drop for Vec<T, N>
439451
where
440452
N: ArrayLength<T>,
@@ -716,6 +728,7 @@ where
716728
mod tests {
717729
use as_slice::AsSlice;
718730
use crate::{consts::*, Vec};
731+
use core::fmt::Write;
719732

720733
#[test]
721734
fn static_new() {
@@ -983,6 +996,13 @@ mod tests {
983996
assert_eq!(v[0], 0);
984997
}
985998

999+
#[test]
1000+
fn write() {
1001+
let mut v: Vec<u8, U4> = Vec::new();
1002+
write!(v, "{:x}", 1234).unwrap();
1003+
assert_eq!(&v[..], b"4d2");
1004+
}
1005+
9861006
#[test]
9871007
fn extend_from_slice() {
9881008
let mut v: Vec<u8, U4> = Vec::new();

0 commit comments

Comments
 (0)