Implement JsonSchema for atomics
This commit is contained in:
parent
60bfc6ee49
commit
cc896739d3
9 changed files with 168 additions and 112 deletions
|
@ -10,6 +10,6 @@ jobs:
|
|||
fast_finish: true
|
||||
script:
|
||||
- cd "${TRAVIS_BUILD_DIR}/schemars"
|
||||
- cargo check --verbose --all --no-default-features
|
||||
- cargo build --verbose --all --all-features
|
||||
- cargo test --verbose --all --all-features --no-fail-fast
|
||||
- cargo check --verbose --no-default-features
|
||||
- cargo build --verbose --all-features
|
||||
- cargo test --verbose --all-features --no-fail-fast
|
||||
|
|
|
@ -9,6 +9,7 @@ license = "MIT"
|
|||
readme = "README.md"
|
||||
keywords = ["rust", "json-schema", "serde"]
|
||||
categories = ["encoding"]
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
schemars_derive = { version = "0.4.0", path = "../schemars_derive" }
|
||||
|
|
67
schemars/build.rs
Normal file
67
schemars/build.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
use std::env;
|
||||
use std::process::Command;
|
||||
use std::str::{self, FromStr};
|
||||
|
||||
// Based on https://github.com/serde-rs/serde/blob/master/serde/build.rs
|
||||
|
||||
fn main() {
|
||||
let minor = match rustc_minor_version() {
|
||||
Some(minor) => minor,
|
||||
None => return,
|
||||
};
|
||||
|
||||
let target = env::var("TARGET").unwrap();
|
||||
let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten";
|
||||
|
||||
// Atomic types, and non-zero signed integers stabilized in Rust 1.34:
|
||||
// https://blog.rust-lang.org/2019/04/11/Rust-1.34.0.html#library-stabilizations
|
||||
if minor >= 34 {
|
||||
println!("cargo:rustc-cfg=num_nonzero_signed");
|
||||
|
||||
// Whitelist of archs that support std::sync::atomic module. Ideally we
|
||||
// would use #[cfg(target_has_atomic = "...")] but it is not stable yet.
|
||||
// Instead this is based on rustc's src/librustc_target/spec/*.rs.
|
||||
let has_atomic64 = target.starts_with("x86_64")
|
||||
|| target.starts_with("i686")
|
||||
|| target.starts_with("aarch64")
|
||||
|| target.starts_with("powerpc64")
|
||||
|| target.starts_with("sparc64")
|
||||
|| target.starts_with("mips64el");
|
||||
let has_atomic32 = has_atomic64 || emscripten;
|
||||
if has_atomic64 {
|
||||
println!("cargo:rustc-cfg=std_atomic64");
|
||||
}
|
||||
if has_atomic32 {
|
||||
println!("cargo:rustc-cfg=std_atomic");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rustc_minor_version() -> Option<u32> {
|
||||
let rustc = match env::var_os("RUSTC") {
|
||||
Some(rustc) => rustc,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
let output = match Command::new(rustc).arg("--version").output() {
|
||||
Ok(output) => output,
|
||||
Err(_) => return None,
|
||||
};
|
||||
|
||||
let version = match str::from_utf8(&output.stdout) {
|
||||
Ok(version) => version,
|
||||
Err(_) => return None,
|
||||
};
|
||||
|
||||
let mut pieces = version.split('.');
|
||||
if pieces.next() != Some("rustc 1") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let next = match pieces.next() {
|
||||
Some(next) => next,
|
||||
None => return None,
|
||||
};
|
||||
|
||||
u32::from_str(next).ok()
|
||||
}
|
47
schemars/src/json_schema_impls/atomic.rs
Normal file
47
schemars/src/json_schema_impls/atomic.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
use crate::gen::SchemaGenerator;
|
||||
use crate::schema::*;
|
||||
use crate::JsonSchema;
|
||||
use std::sync::atomic::*;
|
||||
|
||||
forward_impl!(AtomicBool => bool);
|
||||
|
||||
forward_impl!(AtomicI8 => i8);
|
||||
forward_impl!(AtomicI16 => i16);
|
||||
forward_impl!(AtomicI32 => i32);
|
||||
#[cfg(std_atomic64)]
|
||||
forward_impl!(AtomicI64 => i64);
|
||||
forward_impl!(AtomicIsize => isize);
|
||||
|
||||
forward_impl!(AtomicU8 => u8);
|
||||
forward_impl!(AtomicU16 => u16);
|
||||
forward_impl!(AtomicU32 => u32);
|
||||
#[cfg(std_atomic64)]
|
||||
forward_impl!(AtomicU64 => u64);
|
||||
forward_impl!(AtomicUsize => usize);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::tests::schema_object_for;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn schema_for_atomics() {
|
||||
let atomic_schema = schema_object_for::<(
|
||||
AtomicBool,
|
||||
AtomicI8,
|
||||
AtomicI16,
|
||||
AtomicI32,
|
||||
AtomicI64,
|
||||
AtomicIsize,
|
||||
AtomicU8,
|
||||
AtomicU16,
|
||||
AtomicU32,
|
||||
AtomicU64,
|
||||
AtomicUsize,
|
||||
)>();
|
||||
let basic_schema =
|
||||
schema_object_for::<(bool, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize)>();
|
||||
assert_eq!(atomic_schema, basic_schema);
|
||||
}
|
||||
}
|
|
@ -119,39 +119,11 @@ impl<T: JsonSchema> JsonSchema for Range<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: JsonSchema> JsonSchema for RangeInclusive<T> {
|
||||
fn schema_name() -> String {
|
||||
<Range<T>>::schema_name()
|
||||
}
|
||||
forward_impl!((<T: JsonSchema> JsonSchema for RangeInclusive<T>) => Range<T>);
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
<Range<T>>::json_schema(gen)
|
||||
}
|
||||
}
|
||||
forward_impl!((<T: ?Sized> JsonSchema for std::marker::PhantomData<T>) => ());
|
||||
|
||||
impl<T: ?Sized> JsonSchema for std::marker::PhantomData<T> {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
<()>::schema_name()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
<()>::json_schema(gen)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> JsonSchema for std::fmt::Arguments<'a> {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
<String>::schema_name()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
<String>::json_schema(gen)
|
||||
}
|
||||
}
|
||||
forward_impl!((<'a> JsonSchema for std::fmt::Arguments<'a>) => String);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
|
|
@ -31,36 +31,7 @@ impl JsonSchema for OsString {
|
|||
}
|
||||
}
|
||||
|
||||
impl JsonSchema for OsStr {
|
||||
fn schema_name() -> String {
|
||||
<OsString>::schema_name()
|
||||
}
|
||||
forward_impl!(OsStr => OsString);
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
<OsString>::json_schema(gen)
|
||||
}
|
||||
}
|
||||
|
||||
impl JsonSchema for CString {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
<Vec<u8>>::schema_name()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
<Vec<u8>>::json_schema(gen)
|
||||
}
|
||||
}
|
||||
|
||||
impl JsonSchema for CStr {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
<Vec<u8>>::schema_name()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
<Vec<u8>>::json_schema(gen)
|
||||
}
|
||||
}
|
||||
forward_impl!(CString => Vec<u8>);
|
||||
forward_impl!(CStr => Vec<u8>);
|
||||
|
|
|
@ -6,6 +6,31 @@ macro_rules! no_ref_schema {
|
|||
};
|
||||
}
|
||||
|
||||
macro_rules! forward_impl {
|
||||
(($($impl:tt)+) => $target:ty) => {
|
||||
impl $($impl)+ {
|
||||
fn is_referenceable() -> bool {
|
||||
<$target>::is_referenceable()
|
||||
}
|
||||
|
||||
fn schema_name() -> String {
|
||||
<$target>::schema_name()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
<$target>::json_schema(gen)
|
||||
}
|
||||
|
||||
fn json_schema_optional(gen: &mut SchemaGenerator) -> Schema {
|
||||
<$target>::json_schema_optional(gen)
|
||||
}
|
||||
}
|
||||
};
|
||||
($ty:ty => $target:ty) => {
|
||||
forward_impl!((JsonSchema for $ty) => $target);
|
||||
};
|
||||
}
|
||||
|
||||
mod array;
|
||||
#[cfg(feature = "chrono")]
|
||||
mod chrono;
|
||||
|
@ -18,3 +43,5 @@ mod serdejson;
|
|||
mod time;
|
||||
mod tuple;
|
||||
mod wrapper;
|
||||
#[cfg(std_atomic)]
|
||||
mod atomic;
|
|
@ -16,17 +16,7 @@ impl JsonSchema for Value {
|
|||
}
|
||||
}
|
||||
|
||||
impl JsonSchema for Map<String, Value> {
|
||||
no_ref_schema!();
|
||||
|
||||
fn schema_name() -> String {
|
||||
BTreeMap::<String, Value>::schema_name()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
BTreeMap::<String, Value>::json_schema(gen)
|
||||
}
|
||||
}
|
||||
forward_impl!(Map<String, Value> => BTreeMap::<String, Value>);
|
||||
|
||||
impl JsonSchema for Number {
|
||||
no_ref_schema!();
|
||||
|
|
|
@ -2,42 +2,23 @@ use crate::gen::SchemaGenerator;
|
|||
use crate::schema::Schema;
|
||||
use crate::JsonSchema;
|
||||
|
||||
macro_rules! deref_impl {
|
||||
macro_rules! wrapper_impl {
|
||||
($($desc:tt)+) => {
|
||||
impl $($desc)+
|
||||
where
|
||||
T: JsonSchema,
|
||||
{
|
||||
fn is_referenceable() -> bool {
|
||||
T::is_referenceable()
|
||||
}
|
||||
|
||||
fn schema_name() -> String {
|
||||
T::schema_name()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
T::json_schema(gen)
|
||||
}
|
||||
|
||||
fn json_schema_optional(gen: &mut SchemaGenerator) -> Schema {
|
||||
T::json_schema_optional(gen)
|
||||
}
|
||||
}
|
||||
forward_impl!(($($desc)+ where T: JsonSchema) => T);
|
||||
};
|
||||
}
|
||||
|
||||
deref_impl!(<'a, T: ?Sized> JsonSchema for &'a T);
|
||||
deref_impl!(<'a, T: ?Sized> JsonSchema for &'a mut T);
|
||||
deref_impl!(<T: ?Sized> JsonSchema for Box<T>);
|
||||
deref_impl!(<T: ?Sized> JsonSchema for std::rc::Rc<T>);
|
||||
deref_impl!(<T: ?Sized> JsonSchema for std::rc::Weak<T>);
|
||||
deref_impl!(<T: ?Sized> JsonSchema for std::sync::Arc<T>);
|
||||
deref_impl!(<T: ?Sized> JsonSchema for std::sync::Weak<T>);
|
||||
deref_impl!(<T: ?Sized> JsonSchema for std::sync::Mutex<T>);
|
||||
deref_impl!(<T: ?Sized> JsonSchema for std::sync::RwLock<T>);
|
||||
deref_impl!(<T: ?Sized> JsonSchema for std::cell::Cell<T>);
|
||||
deref_impl!(<T: ?Sized> JsonSchema for std::cell::RefCell<T>);
|
||||
deref_impl!(<'a, T: ?Sized + ToOwned> JsonSchema for std::borrow::Cow<'a, T>);
|
||||
deref_impl!(<T> JsonSchema for std::num::Wrapping<T>);
|
||||
deref_impl!(<T> JsonSchema for std::cmp::Reverse<T>);
|
||||
wrapper_impl!(<'a, T: ?Sized> JsonSchema for &'a T);
|
||||
wrapper_impl!(<'a, T: ?Sized> JsonSchema for &'a mut T);
|
||||
wrapper_impl!(<T: ?Sized> JsonSchema for Box<T>);
|
||||
wrapper_impl!(<T: ?Sized> JsonSchema for std::rc::Rc<T>);
|
||||
wrapper_impl!(<T: ?Sized> JsonSchema for std::rc::Weak<T>);
|
||||
wrapper_impl!(<T: ?Sized> JsonSchema for std::sync::Arc<T>);
|
||||
wrapper_impl!(<T: ?Sized> JsonSchema for std::sync::Weak<T>);
|
||||
wrapper_impl!(<T: ?Sized> JsonSchema for std::sync::Mutex<T>);
|
||||
wrapper_impl!(<T: ?Sized> JsonSchema for std::sync::RwLock<T>);
|
||||
wrapper_impl!(<T: ?Sized> JsonSchema for std::cell::Cell<T>);
|
||||
wrapper_impl!(<T: ?Sized> JsonSchema for std::cell::RefCell<T>);
|
||||
wrapper_impl!(<'a, T: ?Sized + ToOwned> JsonSchema for std::borrow::Cow<'a, T>);
|
||||
wrapper_impl!(<T> JsonSchema for std::num::Wrapping<T>);
|
||||
wrapper_impl!(<T> JsonSchema for std::cmp::Reverse<T>);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue