Skip to content

Commit ad4fc71

Browse files
1 parent 06ba425 commit ad4fc71

7 files changed

Lines changed: 75 additions & 15 deletions

File tree

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ generic-array = { version = "1.1.1", optional = true, default-features = false }
2424
bin-proto = { version = "0.12.5", optional = true, default-features = false }
2525
# Provides `Format` implementations
2626
defmt = { version = "1.0", optional = true }
27+
# Provides `JsonSchema` implementations
28+
schemars = { version = "1.0", optional = true, default-features = false }
2729

2830

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

87+
schemars = ["dep:schemars", "alloc"]
88+
8589
[package.metadata.docs.rs]
86-
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "defmt", "bin-proto"]
90+
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "defmt", "bin-proto", "schemars"]
8791
rustdoc-args = ["--cfg","docs_rs"]
8892

8993
[package.metadata.playground]
90-
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh"]
94+
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "schemars"]
9195

9296
[profile.dev]
9397
lto = "thin"

src/arrayvec.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,33 @@ where
362362
}
363363
}
364364

365+
#[cfg(feature = "schemars")]
366+
#[cfg_attr(docs_rs, doc(cfg(feature = "schemars")))]
367+
impl<A> schemars::JsonSchema for ArrayVec<A>
368+
where
369+
A: Array,
370+
<A as Array>::Item: schemars::JsonSchema,
371+
{
372+
fn schema_name() -> alloc::borrow::Cow<'static, str> {
373+
alloc::format!(
374+
"Array_up_to_size_{}_of_{}",
375+
A::CAPACITY,
376+
<A as Array>::Item::schema_name()
377+
)
378+
.into()
379+
}
380+
381+
fn json_schema(
382+
generator: &mut schemars::SchemaGenerator,
383+
) -> schemars::Schema {
384+
schemars::json_schema!({
385+
"type": "array",
386+
"items": generator.subschema_for::<<A as Array>::Item>(),
387+
"maxItems": A::CAPACITY
388+
})
389+
}
390+
}
391+
365392
impl<A: Array> ArrayVec<A> {
366393
/// Move all values from `other` into this vec.
367394
///
@@ -493,7 +520,7 @@ impl<A: Array> ArrayVec<A> {
493520
/// assert_eq!(av2.as_slice(), &[2, 3][..]);
494521
///
495522
/// av.drain(..);
496-
/// assert_eq!(av.as_slice(), &[]);
523+
/// assert_eq!(av.as_slice(), &[] as &[i32]);
497524
/// ```
498525
#[inline]
499526
pub fn drain<R>(&mut self, range: R) -> ArrayVecDrain<'_, A::Item>
@@ -758,7 +785,7 @@ impl<A: Array> ArrayVec<A> {
758785
/// ```rust
759786
/// # use tinyvec::*;
760787
/// let mut av = array_vec!([i32; 2]);
761-
/// assert_eq!(&av[..], []);
788+
/// assert_eq!(&av[..], [] as [i32; 0]);
762789
/// av.push(1);
763790
/// assert_eq!(&av[..], [1]);
764791
/// av.push(2);
@@ -777,7 +804,7 @@ impl<A: Array> ArrayVec<A> {
777804
/// ```rust
778805
/// # use tinyvec::*;
779806
/// let mut av = array_vec!([i32; 2]);
780-
/// assert_eq!(av.as_slice(), []);
807+
/// assert_eq!(av.as_slice(), [] as [i32; 0]);
781808
/// assert_eq!(av.try_push(1), None);
782809
/// assert_eq!(&av[..], [1]);
783810
/// assert_eq!(av.try_push(2), None);
@@ -1090,7 +1117,7 @@ impl<A: Array> ArrayVec<A> {
10901117
/// assert_eq!(av2.as_slice(), &[2, 3][..]);
10911118
///
10921119
/// av.splice(.., None);
1093-
/// assert_eq!(av.as_slice(), &[]);
1120+
/// assert_eq!(av.as_slice(), &[] as &[i32]);
10941121
/// ```
10951122
#[inline]
10961123
pub fn splice<R, I>(
@@ -1246,7 +1273,7 @@ impl<A> ArrayVec<A> {
12461273
/// ```rust
12471274
/// # use tinyvec::ArrayVec;
12481275
/// let mut data = ArrayVec::from_array_empty([1, 2, 3, 4]);
1249-
/// assert_eq!(&data[..], &[]);
1276+
/// assert_eq!(&data[..], &[] as &[i32]);
12501277
/// data.push(42);
12511278
/// assert_eq!(&data[..], &[42]);
12521279
/// ```

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
//! implementation.
5858
//! * `defmt` provides a `Format` implementation for all types, provided the
5959
//! inner item also has an implementation.
60+
//! * `schemars` provides a `JsonSchema` implementation for [`TinyVec`] and
61+
//! [`ArrayVec`] types, provided the inner item also has an implementation.
6062
//!
6163
//! ## API
6264
//! The general goal of the crate is that, as much as possible, the vecs here

src/slicevec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'s, T> SliceVec<'s, T> {
143143
/// assert_eq!(drained_values.as_slice(), &[7, 8][..]);
144144
///
145145
/// sv.drain(..);
146-
/// assert_eq!(sv.as_slice(), &[]);
146+
/// assert_eq!(sv.as_slice(), &[] as &[i32]);
147147
/// ```
148148
#[inline]
149149
pub fn drain<'p, R: RangeBounds<usize>>(
@@ -345,7 +345,7 @@ impl<'s, T> SliceVec<'s, T> {
345345
/// # use tinyvec::*;
346346
/// let mut arr = [0, 0];
347347
/// let mut sv = SliceVec::from_slice_len(&mut arr, 0);
348-
/// assert_eq!(&sv[..], []);
348+
/// assert_eq!(&sv[..], [] as [i32; 0]);
349349
/// sv.push(1);
350350
/// assert_eq!(&sv[..], [1]);
351351
/// sv.push(2);

src/tinyvec.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,33 @@ where
360360
}
361361
}
362362

363+
#[cfg(feature = "schemars")]
364+
#[cfg_attr(docs_rs, doc(cfg(feature = "schemars")))]
365+
impl<A> schemars::JsonSchema for TinyVec<A>
366+
where
367+
A: Array,
368+
<A as Array>::Item: schemars::JsonSchema,
369+
{
370+
fn schema_name() -> alloc::borrow::Cow<'static, str> {
371+
alloc::format!(
372+
"Array_up_to_size_{}_of_{}",
373+
A::CAPACITY,
374+
<A as Array>::Item::schema_name()
375+
)
376+
.into()
377+
}
378+
379+
fn json_schema(
380+
generator: &mut schemars::SchemaGenerator,
381+
) -> schemars::Schema {
382+
schemars::json_schema!({
383+
"type": "array",
384+
"items": generator.subschema_for::<<A as Array>::Item>(),
385+
"maxItems": A::CAPACITY
386+
})
387+
}
388+
}
389+
363390
impl<A: Array> TinyVec<A> {
364391
/// Returns whether elements are on heap
365392
#[inline(always)]
@@ -933,7 +960,7 @@ impl<A: Array> TinyVec<A> {
933960
/// assert_eq!(tv2.as_slice(), &[2, 3][..]);
934961
///
935962
/// tv.drain(..);
936-
/// assert_eq!(tv.as_slice(), &[]);
963+
/// assert_eq!(tv.as_slice(), &[] as &[i32]);
937964
/// ```
938965
#[inline]
939966
pub fn drain<R: RangeBounds<usize>>(
@@ -1188,7 +1215,7 @@ impl<A: Array> TinyVec<A> {
11881215
/// assert_eq!(tv2.as_slice(), &[2, 3][..]);
11891216
///
11901217
/// tv.splice(.., None);
1191-
/// assert_eq!(tv.as_slice(), &[]);
1218+
/// assert_eq!(tv.as_slice(), &[] as &[i32]);
11921219
/// ```
11931220
#[inline]
11941221
pub fn splice<R, I>(

tests/arrayvec.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn ArrayVec_append() {
132132
//
133133
av.append(&mut av2);
134134
assert_eq!(av.as_slice(), &[1_i32, 2, 3, 4, 5, 6]);
135-
assert_eq!(av2.as_slice(), &[]);
135+
assert_eq!(av2.as_slice(), &[] as &[i32]);
136136
}
137137

138138
#[test]
@@ -167,7 +167,7 @@ fn ArrayVec_swap_remove() {
167167
assert_eq!(av.swap_remove(0), 3);
168168
assert_eq!(&av[..], &[2][..]);
169169
assert_eq!(av.swap_remove(0), 2);
170-
assert_eq!(&av[..], &[][..]);
170+
assert_eq!(&av[..], &[][..] as &[i32]);
171171
}
172172

173173
#[test]
@@ -552,7 +552,7 @@ fn ArrayVec_try_from_slice() {
552552

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

557557
let fits: Result<ArrayVec<[i32; 2]>, _> = ArrayVec::try_from(&nums[..2]);
558558
assert!(fits.is_ok());

tests/tinyvec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn TinyVec_swap_remove() {
2323
assert_eq!(tv.swap_remove(0), 3);
2424
assert_eq!(&tv[..], &[2][..]);
2525
assert_eq!(tv.swap_remove(0), 2);
26-
assert_eq!(&tv[..], &[][..]);
26+
assert_eq!(&tv[..], &[][..] as &[i32]);
2727
}
2828

2929
#[test]

0 commit comments

Comments
 (0)