diff --git a/Cargo.toml b/Cargo.toml
index 82fbade..29609db 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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" }
diff --git a/src/arrayvec.rs b/src/arrayvec.rs
index 3f37ce8..9ffe9e4 100644
--- a/src/arrayvec.rs
+++ b/src/arrayvec.rs
@@ -1024,6 +1024,26 @@ where
}
}
+#[cfg(feature = "experimental_write_impl")]
+impl core::fmt::Write for ArrayVec
+where
+ A: Array,
+ A::Item: From,
+{
+ 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
// // // // // // // //