Skip to content

Commit

Permalink
Fixed smallvec ToBoundedStatic, IntoBoundedStatic to process elements…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
xkikeg committed Jul 26, 2024
1 parent 6cdaf2b commit d1e2e37
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions bounded-static/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,29 +824,33 @@ impl IntoBoundedStatic for smol_str::SmolStr {

/// [`ToBoundedStatic`] impl for `smallvec::SmallVec`.
#[cfg(feature = "smallvec")]
impl<A> ToBoundedStatic for smallvec::SmallVec<A>
impl<T, const N: usize> ToBoundedStatic for smallvec::SmallVec<[T; N]>
where
A: smallvec::Array + 'static,
A::Item: Clone,
[T; N]: smallvec::Array<Item = T>,
[T::Static; N]: smallvec::Array<Item = T::Static>,
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<A> IntoBoundedStatic for smallvec::SmallVec<A>
impl<T, const N: usize> IntoBoundedStatic for smallvec::SmallVec<[T; N]>
where
A: smallvec::Array + 'static,
A::Item: Clone,
[T; N]: smallvec::Array<Item = T>,
[T::Static; N]: smallvec::Array<Item = T::Static>,
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()
}
}

Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit d1e2e37

Please sign in to comment.