mirror of
https://github.com/openharmony/third_party_rust_tinyvec.git
synced 2026-07-19 14:23:33 -04:00
put in a highly experimental Write impl for ArrayVec of bytes
This commit is contained in:
@@ -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" }
|
||||
|
||||
@@ -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
|
||||
// // // // // // // //
|
||||
|
||||
Reference in New Issue
Block a user