Skip to content

Commit d1e2e37

Browse files
committed
Fixed smallvec ToBoundedStatic, IntoBoundedStatic to process elements inside.
Now there's one drawback that in some case SmallVec<A> 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<T, N> instead of SmallVec<[T; N]> with removing this. Context: servo/rust-smallvec#183
1 parent 6cdaf2b commit d1e2e37

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

bounded-static/src/lib.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -824,29 +824,33 @@ impl IntoBoundedStatic for smol_str::SmolStr {
824824

825825
/// [`ToBoundedStatic`] impl for `smallvec::SmallVec`.
826826
#[cfg(feature = "smallvec")]
827-
impl<A> ToBoundedStatic for smallvec::SmallVec<A>
827+
impl<T, const N: usize> ToBoundedStatic for smallvec::SmallVec<[T; N]>
828828
where
829-
A: smallvec::Array + 'static,
830-
A::Item: Clone,
829+
[T; N]: smallvec::Array<Item = T>,
830+
[T::Static; N]: smallvec::Array<Item = T::Static>,
831+
T: ToBoundedStatic,
831832
{
832-
type Static = Self;
833+
type Static = smallvec::SmallVec<[T::Static; N]>;
833834

834835
fn to_static(&self) -> Self::Static {
835-
self.clone()
836+
self.iter().map(ToBoundedStatic::to_static).collect()
836837
}
837838
}
838839

839-
/// No-op [`IntoBoundedStatic`] impl for `smallvec::SmallVec`.
840+
/// [`IntoBoundedStatic`] impl for `smallvec::SmallVec`.
840841
#[cfg(feature = "smallvec")]
841-
impl<A> IntoBoundedStatic for smallvec::SmallVec<A>
842+
impl<T, const N: usize> IntoBoundedStatic for smallvec::SmallVec<[T; N]>
842843
where
843-
A: smallvec::Array + 'static,
844-
A::Item: Clone,
844+
[T; N]: smallvec::Array<Item = T>,
845+
[T::Static; N]: smallvec::Array<Item = T::Static>,
846+
T: IntoBoundedStatic,
845847
{
846-
type Static = Self;
848+
type Static = smallvec::SmallVec<[T::Static; N]>;
847849

848850
fn into_static(self) -> Self::Static {
849-
self
851+
self.into_iter()
852+
.map(IntoBoundedStatic::into_static)
853+
.collect()
850854
}
851855
}
852856

@@ -1706,6 +1710,23 @@ mod smallvec_tests {
17061710
ensure_static(small_vec.to_static());
17071711
ensure_static(small_vec.into_static());
17081712
}
1713+
1714+
#[cfg(feature = "alloc")]
1715+
mod alloc_smallvec_tests {
1716+
use super::*;
1717+
1718+
use crate::std::string::ToString;
1719+
1720+
#[test]
1721+
fn test_smallvec3() {
1722+
let x = "foo".to_string();
1723+
let y = "bar".to_string();
1724+
let buf = [Cow::Borrowed(x.as_str()), Cow::Borrowed(y.as_str())];
1725+
let small_vec: smallvec::SmallVec<_> = smallvec::SmallVec::from_buf(buf);
1726+
ensure_static(small_vec.to_static());
1727+
ensure_static(small_vec.into_static());
1728+
}
1729+
}
17091730
}
17101731

17111732
#[cfg(feature = "smartstring")]

0 commit comments

Comments
 (0)