Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ generic-array = { version = "1.1.1", optional = true, default-features = false }
bin-proto = { version = "0.12.5", optional = true, default-features = false }
# Provides `Format` implementations
defmt = { version = "1.0", optional = true }
# Provides `JsonSchema` implementations
schemars = { version = "1.0", optional = true, default-features = false }


[features]
Expand Down Expand Up @@ -82,12 +84,14 @@ experimental_write_impl = []
# and thus a nightly compiler, but is only used in benchmarks.
real_blackbox = ["criterion/real_blackbox"]

schemars = ["dep:schemars", "alloc"]

[package.metadata.docs.rs]
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "defmt", "bin-proto"]
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "defmt", "bin-proto", "schemars"]
rustdoc-args = ["--cfg","docs_rs"]

[package.metadata.playground]
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh"]
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "schemars"]

[profile.dev]
lto = "thin"
Expand Down
37 changes: 32 additions & 5 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,33 @@ where
}
}

#[cfg(feature = "schemars")]
#[cfg_attr(docs_rs, doc(cfg(feature = "schemars")))]
impl<A> schemars::JsonSchema for ArrayVec<A>
where
A: Array,
<A as Array>::Item: schemars::JsonSchema,
{
fn schema_name() -> alloc::borrow::Cow<'static, str> {
alloc::format!(
"Array_up_to_size_{}_of_{}",
A::CAPACITY,
<A as Array>::Item::schema_name()
)
.into()
}

fn json_schema(
generator: &mut schemars::SchemaGenerator,
) -> schemars::Schema {
schemars::json_schema!({
"type": "array",
"items": generator.subschema_for::<<A as Array>::Item>(),
"maxItems": A::CAPACITY
})
}
}

impl<A: Array> ArrayVec<A> {
/// Move all values from `other` into this vec.
///
Expand Down Expand Up @@ -493,7 +520,7 @@ impl<A: Array> ArrayVec<A> {
/// assert_eq!(av2.as_slice(), &[2, 3][..]);
///
/// av.drain(..);
/// assert_eq!(av.as_slice(), &[]);
/// assert_eq!(av.as_slice(), &[] as &[i32]);
/// ```
#[inline]
pub fn drain<R>(&mut self, range: R) -> ArrayVecDrain<'_, A::Item>
Expand Down Expand Up @@ -758,7 +785,7 @@ impl<A: Array> ArrayVec<A> {
/// ```rust
/// # use tinyvec::*;
/// let mut av = array_vec!([i32; 2]);
/// assert_eq!(&av[..], []);
/// assert_eq!(&av[..], [] as [i32; 0]);
/// av.push(1);
/// assert_eq!(&av[..], [1]);
/// av.push(2);
Expand All @@ -777,7 +804,7 @@ impl<A: Array> ArrayVec<A> {
/// ```rust
/// # use tinyvec::*;
/// let mut av = array_vec!([i32; 2]);
/// assert_eq!(av.as_slice(), []);
/// assert_eq!(av.as_slice(), [] as [i32; 0]);
/// assert_eq!(av.try_push(1), None);
/// assert_eq!(&av[..], [1]);
/// assert_eq!(av.try_push(2), None);
Expand Down Expand Up @@ -1090,7 +1117,7 @@ impl<A: Array> ArrayVec<A> {
/// assert_eq!(av2.as_slice(), &[2, 3][..]);
///
/// av.splice(.., None);
/// assert_eq!(av.as_slice(), &[]);
/// assert_eq!(av.as_slice(), &[] as &[i32]);
/// ```
#[inline]
pub fn splice<R, I>(
Expand Down Expand Up @@ -1246,7 +1273,7 @@ impl<A> ArrayVec<A> {
/// ```rust
/// # use tinyvec::ArrayVec;
/// let mut data = ArrayVec::from_array_empty([1, 2, 3, 4]);
/// assert_eq!(&data[..], &[]);
/// assert_eq!(&data[..], &[] as &[i32]);
/// data.push(42);
/// assert_eq!(&data[..], &[42]);
/// ```
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
//! implementation.
//! * `defmt` provides a `Format` implementation for all types, provided the
//! inner item also has an implementation.
//! * `schemars` provides a `JsonSchema` implementation for [`TinyVec`] and
//! [`ArrayVec`] types, provided the inner item also has an implementation.
//!
//! ## API
//! The general goal of the crate is that, as much as possible, the vecs here
Expand Down
4 changes: 2 additions & 2 deletions src/slicevec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<'s, T> SliceVec<'s, T> {
/// assert_eq!(drained_values.as_slice(), &[7, 8][..]);
///
/// sv.drain(..);
/// assert_eq!(sv.as_slice(), &[]);
/// assert_eq!(sv.as_slice(), &[] as &[i32]);
/// ```
#[inline]
pub fn drain<'p, R: RangeBounds<usize>>(
Expand Down Expand Up @@ -345,7 +345,7 @@ impl<'s, T> SliceVec<'s, T> {
/// # use tinyvec::*;
/// let mut arr = [0, 0];
/// let mut sv = SliceVec::from_slice_len(&mut arr, 0);
/// assert_eq!(&sv[..], []);
/// assert_eq!(&sv[..], [] as [i32; 0]);
/// sv.push(1);
/// assert_eq!(&sv[..], [1]);
/// sv.push(2);
Expand Down
31 changes: 29 additions & 2 deletions src/tinyvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,33 @@ where
}
}

#[cfg(feature = "schemars")]
#[cfg_attr(docs_rs, doc(cfg(feature = "schemars")))]
impl<A> schemars::JsonSchema for TinyVec<A>
where
A: Array,
<A as Array>::Item: schemars::JsonSchema,
{
fn schema_name() -> alloc::borrow::Cow<'static, str> {
alloc::format!(
"Array_up_to_size_{}_of_{}",
A::CAPACITY,
<A as Array>::Item::schema_name()
)
.into()
}

fn json_schema(
generator: &mut schemars::SchemaGenerator,
) -> schemars::Schema {
schemars::json_schema!({
"type": "array",
"items": generator.subschema_for::<<A as Array>::Item>(),
"maxItems": A::CAPACITY
})
}
}

impl<A: Array> TinyVec<A> {
/// Returns whether elements are on heap
#[inline(always)]
Expand Down Expand Up @@ -912,7 +939,7 @@ impl<A: Array> TinyVec<A> {
/// assert_eq!(tv2.as_slice(), &[2, 3][..]);
///
/// tv.drain(..);
/// assert_eq!(tv.as_slice(), &[]);
/// assert_eq!(tv.as_slice(), &[] as &[i32]);
/// ```
#[inline]
pub fn drain<R: RangeBounds<usize>>(
Expand Down Expand Up @@ -1167,7 +1194,7 @@ impl<A: Array> TinyVec<A> {
/// assert_eq!(tv2.as_slice(), &[2, 3][..]);
///
/// tv.splice(.., None);
/// assert_eq!(tv.as_slice(), &[]);
/// assert_eq!(tv.as_slice(), &[] as &[i32]);
/// ```
#[inline]
pub fn splice<R, I>(
Expand Down
6 changes: 3 additions & 3 deletions tests/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn ArrayVec_append() {
//
av.append(&mut av2);
assert_eq!(av.as_slice(), &[1_i32, 2, 3, 4, 5, 6]);
assert_eq!(av2.as_slice(), &[]);
assert_eq!(av2.as_slice(), &[] as &[i32]);
}

#[test]
Expand Down Expand Up @@ -167,7 +167,7 @@ fn ArrayVec_swap_remove() {
assert_eq!(av.swap_remove(0), 3);
assert_eq!(&av[..], &[2][..]);
assert_eq!(av.swap_remove(0), 2);
assert_eq!(&av[..], &[][..]);
assert_eq!(&av[..], &[][..] as &[i32]);
}

#[test]
Expand Down Expand Up @@ -552,7 +552,7 @@ fn ArrayVec_try_from_slice() {

let empty: Result<ArrayVec<[i32; 2]>, _> = ArrayVec::try_from(&nums[..0]);
assert!(empty.is_ok());
assert_eq!(empty.unwrap().as_slice(), &[]);
assert_eq!(empty.unwrap().as_slice(), &[] as &[i32]);

let fits: Result<ArrayVec<[i32; 2]>, _> = ArrayVec::try_from(&nums[..2]);
assert!(fits.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion tests/tinyvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn TinyVec_swap_remove() {
assert_eq!(tv.swap_remove(0), 3);
assert_eq!(&tv[..], &[2][..]);
assert_eq!(tv.swap_remove(0), 2);
assert_eq!(&tv[..], &[][..]);
assert_eq!(&tv[..], &[][..] as &[i32]);
}

#[test]
Expand Down
Loading