put in a highly experimental Write impl for ArrayVec of bytes

This commit is contained in:
Lokathor
2020-02-10 20:02:56 -07:00
parent 49a26a2bd5
commit 3b06e8498c
2 changed files with 24 additions and 0 deletions
+4
View File
@@ -30,6 +30,10 @@ nightly_slice_partition_dedup = []
# use const generics for arrays
nightly_const_generics = []
# NOT considered part of the crate's SemVer!!!
# This experimental feature adds `core::fmt::Write` to ArrayVec.
experimental_write_impl = []
[badges]
appveyor = { repository = "Lokathor/tinyvec" }
travis-ci = { repository = "Lokathor/tinyvec" }
+20
View File
@@ -1024,6 +1024,26 @@ where
}
}
#[cfg(feature = "experimental_write_impl")]
impl<A> core::fmt::Write for ArrayVec<A>
where
A: Array,
A::Item: From<u8>,
{
fn write_str(&mut self, s: &str) -> core::fmt::Result {
if self.len() + s.as_bytes().len() > A::CAPACITY {
return Err(core::fmt::Error)
}
let mut buf = [0; 4];
for c in s.chars() {
for b in c.encode_utf8(&mut buf) {
self.push(*b);
}
}
Ok(())
}
}
// // // // // // // //
// Formatting impls
// // // // // // // //