From d1e2e375836e05a6a9c615b42a6cbff049b927ac Mon Sep 17 00:00:00 2001 From: Katsuaki Ikegami Date: Fri, 26 Jul 2024 15:56:21 +0200 Subject: [PATCH] Fixed smallvec ToBoundedStatic, IntoBoundedStatic to process elements inside. Now there's one drawback that in some case SmallVec won't be supported where A is still smallvec::Array but isn't [T; N]. However, it should be OK in reality, given smallvec itself is going to be SmallVec instead of SmallVec<[T; N]> with removing this. Context: https://github.com/servo/rust-smallvec/issues/183 --- bounded-static/src/lib.rs | 43 +++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/bounded-static/src/lib.rs b/bounded-static/src/lib.rs index bc763fe..6da8c41 100644 --- a/bounded-static/src/lib.rs +++ b/bounded-static/src/lib.rs @@ -824,29 +824,33 @@ impl IntoBoundedStatic for smol_str::SmolStr { /// [`ToBoundedStatic`] impl for `smallvec::SmallVec`. #[cfg(feature = "smallvec")] -impl ToBoundedStatic for smallvec::SmallVec +impl ToBoundedStatic for smallvec::SmallVec<[T; N]> where - A: smallvec::Array + 'static, - A::Item: Clone, + [T; N]: smallvec::Array, + [T::Static; N]: smallvec::Array, + T: ToBoundedStatic, { - type Static = Self; + type Static = smallvec::SmallVec<[T::Static; N]>; fn to_static(&self) -> Self::Static { - self.clone() + self.iter().map(ToBoundedStatic::to_static).collect() } } -/// No-op [`IntoBoundedStatic`] impl for `smallvec::SmallVec`. +/// [`IntoBoundedStatic`] impl for `smallvec::SmallVec`. #[cfg(feature = "smallvec")] -impl IntoBoundedStatic for smallvec::SmallVec +impl IntoBoundedStatic for smallvec::SmallVec<[T; N]> where - A: smallvec::Array + 'static, - A::Item: Clone, + [T; N]: smallvec::Array, + [T::Static; N]: smallvec::Array, + T: IntoBoundedStatic, { - type Static = Self; + type Static = smallvec::SmallVec<[T::Static; N]>; fn into_static(self) -> Self::Static { - self + self.into_iter() + .map(IntoBoundedStatic::into_static) + .collect() } } @@ -1706,6 +1710,23 @@ mod smallvec_tests { ensure_static(small_vec.to_static()); ensure_static(small_vec.into_static()); } + + #[cfg(feature = "alloc")] + mod alloc_smallvec_tests { + use super::*; + + use crate::std::string::ToString; + + #[test] + fn test_smallvec3() { + let x = "foo".to_string(); + let y = "bar".to_string(); + let buf = [Cow::Borrowed(x.as_str()), Cow::Borrowed(y.as_str())]; + let small_vec: smallvec::SmallVec<_> = smallvec::SmallVec::from_buf(buf); + ensure_static(small_vec.to_static()); + ensure_static(small_vec.into_static()); + } + } } #[cfg(feature = "smartstring")]