From 3b06e8498c2ea29bc39df3ab23e85b886f6447f1 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Mon, 10 Feb 2020 20:02:56 -0700 Subject: [PATCH] put in a highly experimental Write impl for ArrayVec of bytes --- Cargo.toml | 4 ++++ src/arrayvec.rs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) 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 // // // // // // // //