Implement core::fmt::Write for heapless::Vec<u8, N>

This commit is contained in:
ijl 2020-02-11 13:51:25 +00:00
parent 1d6c207917
commit 64e891330c

View File

@ -365,6 +365,18 @@ where
}
}
impl<N> fmt::Write for Vec<u8, N>
where
N: ArrayLength<u8>,
{
fn write_str(&mut self, s: &str) -> fmt::Result {
match self.extend_from_slice(s.as_bytes()) {
Ok(()) => Ok(()),
Err(_) => Err(fmt::Error),
}
}
}
impl<T, N> Drop for Vec<T, N>
where
N: ArrayLength<T>,
@ -645,6 +657,7 @@ where
#[cfg(test)]
mod tests {
use crate::{consts::*, Vec};
use core::fmt::Write;
#[test]
fn static_new() {
@ -911,4 +924,11 @@ mod tests {
v.resize_default(1).unwrap();
assert_eq!(v[0], 0);
}
#[test]
fn write() {
let mut v: Vec<u8, U4> = Vec::new();
write!(v, "{:x}", 1234).unwrap();
assert_eq!(&v[..], b"4d2")
}
}