From 4c1176d2f4d40e9726038e75dc8154cda10e427d Mon Sep 17 00:00:00 2001 From: Katsuaki Ikegami Date: Fri, 26 Jul 2024 15:56:21 +0200 Subject: [PATCH] feat: remove `'static` bound from `T` for `ToBoundedStatic for smallvec::SmallVec<[T; N]>` --- bounded-static/src/lib.rs | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/bounded-static/src/lib.rs b/bounded-static/src/lib.rs index 1b01804..ee59ad4 100644 --- a/bounded-static/src/lib.rs +++ b/bounded-static/src/lib.rs @@ -823,29 +823,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 where - A: smallvec::Array + 'static, - A::Item: Clone, + A: smallvec::Array + ToBoundedStatic, + T: ToBoundedStatic, + ::Static: smallvec::Array, { - type Static = Self; + type Static = smallvec::SmallVec; 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 where - A: smallvec::Array + 'static, - A::Item: Clone, + A: smallvec::Array + IntoBoundedStatic, + T: IntoBoundedStatic, + ::Static: smallvec::Array, { - type Static = Self; + type Static = smallvec::SmallVec; fn into_static(self) -> Self::Static { - self + self.into_iter() + .map(IntoBoundedStatic::into_static) + .collect() } } @@ -1604,6 +1608,16 @@ mod smallvec_tests { ensure_static(small_vec.to_static()); ensure_static(small_vec.into_static()); } + + #[test] + fn test_smallvec3() { + let x = String::from("foo"); + let y = String::from("bar"); + 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")]