diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 4ec05e6..7fd1da7 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -11,7 +11,7 @@ jobs: matrix: os: [ubuntu-latest] rust: - - 1.55.0 # approximate MSRV is Stable -30 + - 1.60.0 # approximate MSRV is Stable -30 - stable - beta - nightly @@ -28,11 +28,11 @@ jobs: toolchain: ${{ matrix.rust }} # On MSRV some dev-dependencies don't build so we can't run tests. - name: Check MSRV - if: matrix.rust == '1.55.0' + if: matrix.rust == '1.60.0' run: | cargo check --features=alloc,std,grab_spare_slice - name: Test non nightly - if: matrix.rust != '1.55.0' && matrix.rust != 'nightly' + if: matrix.rust != '1.60.0' && matrix.rust != 'nightly' run: | cargo test --features=alloc,std,grab_spare_slice,latest_stable_rust - name: Test on Nightly with All Features diff --git a/Cargo.toml b/Cargo.toml index 819fd44..b02d77d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,8 @@ arbitrary = { version = "1", optional = true } borsh = { version = "1.2.0", optional = true, default-features = false } # Implements the trait `Array` for `GenericArray` struct. generic-array = { version = "1.1.1", optional = true, default-features = false } +# Provides `JsonSchema` implementations +schemars = { version = "1.0", optional = true, default-features = false } [features] @@ -75,12 +77,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"] +features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "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.bench] debug = 2 diff --git a/src/arrayvec.rs b/src/arrayvec.rs index c55796d..125c649 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -287,6 +287,33 @@ where } } +#[cfg(feature = "schemars")] +#[cfg_attr(docs_rs, doc(cfg(feature = "schemars")))] +impl schemars::JsonSchema for ArrayVec +where + A: Array, + ::Item: schemars::JsonSchema, +{ + fn schema_name() -> alloc::borrow::Cow<'static, str> { + alloc::format!( + "Array_up_to_size_{}_of_{}", + A::CAPACITY, + ::Item::schema_name() + ) + .into() + } + + fn json_schema( + generator: &mut schemars::SchemaGenerator, + ) -> schemars::Schema { + schemars::json_schema!({ + "type": "array", + "items": generator.subschema_for::<::Item>(), + "maxItems": A::CAPACITY + }) + } +} + impl ArrayVec { /// Move all values from `other` into this vec. /// diff --git a/src/lib.rs b/src/lib.rs index 112853c..eb0c11f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,6 +52,8 @@ //! * `borsh` provides a `BorshSerialize` and `BorshDeserialize` implementation //! for [`TinyVec`] and [`ArrayVec`] 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 diff --git a/src/tinyvec.rs b/src/tinyvec.rs index d2bf218..fd7020b 100644 --- a/src/tinyvec.rs +++ b/src/tinyvec.rs @@ -273,6 +273,33 @@ where } } +#[cfg(feature = "schemars")] +#[cfg_attr(docs_rs, doc(cfg(feature = "schemars")))] +impl schemars::JsonSchema for TinyVec +where + A: Array, + ::Item: schemars::JsonSchema, +{ + fn schema_name() -> alloc::borrow::Cow<'static, str> { + alloc::format!( + "Array_up_to_size_{}_of_{}", + A::CAPACITY, + ::Item::schema_name() + ) + .into() + } + + fn json_schema( + generator: &mut schemars::SchemaGenerator, + ) -> schemars::Schema { + schemars::json_schema!({ + "type": "array", + "items": generator.subschema_for::<::Item>(), + "maxItems": A::CAPACITY + }) + } +} + impl TinyVec { /// Returns whether elements are on heap #[inline(always)]