Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support const generics #205

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
rust:
- 1.45.0
- 1.51.0
- stable
- beta
- nightly
Expand Down
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.45.0"
msrv = "1.51.0"
56 changes: 24 additions & 32 deletions schemars/src/json_schema_impls/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;

pub struct EmptyArray<T>(pub [T; 0]);

// Does not require T: JsonSchema.
impl<T> JsonSchema for [T; 0] {
impl<T> JsonSchema for EmptyArray<T> {
no_ref_schema!();

fn schema_name() -> String {
Expand All @@ -23,39 +25,29 @@ impl<T> JsonSchema for [T; 0] {
}
}

macro_rules! array_impls {
($($len:tt)+) => {
$(
impl<T: JsonSchema> JsonSchema for [T; $len] {
no_ref_schema!();

fn schema_name() -> String {
format!("Array_size_{}_of_{}", $len, T::schema_name())
}
impl<T: JsonSchema, const N: usize> JsonSchema for [T; N] {
no_ref_schema!();

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Array.into()),
array: Some(Box::new(ArrayValidation {
items: Some(gen.subschema_for::<T>().into()),
max_items: Some($len),
min_items: Some($len),
..Default::default()
})),
..Default::default()
}
.into()
}
}
)+
fn schema_name() -> String {
match N {
0 => "EmptyArray".to_owned(),
n => format!("Array_size_{}_of_{}", n, T::schema_name()),
}
}
}

array_impls! {
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::Array.into()),
array: Some(Box::new(ArrayValidation {
items: Some(gen.subschema_for::<T>().into()),
max_items: Some(N as u32),
min_items: Some(N as u32),
..Default::default()
})),
..Default::default()
}
.into()
}
}

#[cfg(test)]
Expand Down Expand Up @@ -85,7 +77,7 @@ mod tests {

#[test]
fn schema_for_empty_array() {
let schema = schema_object_for::<[SomeStruct; 0]>();
let schema = schema_object_for::<EmptyArray<SomeStruct>>();
assert_eq!(
schema.instance_type,
Some(SingleOrVec::from(InstanceType::Array))
Expand Down
1 change: 1 addition & 0 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ macro_rules! forward_impl {
}

mod array;
pub use array::EmptyArray;
#[cfg(feature = "arrayvec05")]
mod arrayvec05;
#[cfg(feature = "arrayvec07")]
Expand Down
1 change: 1 addition & 0 deletions schemars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ pub type MapEntry<'a, K, V> = indexmap::map::Entry<'a, K, V>;

mod flatten;
mod json_schema_impls;
pub use json_schema_impls::EmptyArray;
mod ser;
#[macro_use]
mod macros;
Expand Down