Skip to content
Merged
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
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ malloc-size-of = ["dep:malloc_size_of"]
may-dangle = []
proptest = ["dep:proptest", "std"]
rayon = ["dep:rayon", "std"]
schemars = ["dep:schemars", "std"]
serde = ["dep:serde_core"]
specialization = []
std = []
Expand All @@ -54,6 +55,7 @@ proptest = { version = "1", optional = true, features = [
"std",
], default-features = false }
rayon = { version = "1", optional = true, default-features = false }
schemars = { version = "1", optional = true, default-features = false }
serde_core = { version = "1", optional = true, default-features = false }
smallvec-macro = { version = "0.0.0", path = "./macro", default-features = false }
zeroize = { version = "1", optional = true, default-features = false }
Expand Down Expand Up @@ -91,6 +93,10 @@ required-features = ["may-dangle"]
name = "rayon"
required-features = ["rayon"]

[[test]]
name = "schemars"
required-features = ["schemars"]

[[test]]
name = "serde"
required-features = ["serde"]
Expand Down
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ use std::io::{
};
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
#[cfg(feature = "schemars")]
use {
alloc::borrow::Cow,
schemars::{
JsonSchema,
Schema,
SchemaGenerator
}
};
use {
allocator::{
Allocator,
Expand Down Expand Up @@ -1973,3 +1982,25 @@ impl_rts_array!(
(T, const INLINE: usize, Heap: Allocator);
using len truncate
);

#[cfg(feature = "schemars")]
#[cfg_attr(docsrs, doc(cfg(feature = "scheamrs")))]
impl<Item: JsonSchema, const INLINE: usize, Heap: Allocator> JsonSchema
for SmallVec<Item, INLINE, Heap>
{
fn inline_schema() -> bool {
<alloc::vec::Vec<Item> as JsonSchema>::inline_schema()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it but I don't think we have any alternative

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll think about it

}

fn schema_name() -> Cow<'static, str> {
<alloc::vec::Vec<Item> as JsonSchema>::schema_name()
}

fn schema_id() -> Cow<'static, str> {
<alloc::vec::Vec<Item> as JsonSchema>::schema_id()
}

fn json_schema(generator: &mut SchemaGenerator) -> Schema {
<alloc::vec::Vec<Item> as JsonSchema>::json_schema(generator)
}
}
6 changes: 5 additions & 1 deletion tests/macro.rs
Comment thread
GauravPawar101 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ use smallvec::{
smallvec
};

fn check_eq<T: PartialEq + core::fmt::Debug + ?Sized>(a: &T, b: &T) {
assert_eq!(a, b);
}

#[test]
fn smallvec() {
let mut vec: SmallVec<i32, 2>;

macro_rules! check {
($init:tt) => {
vec = smallvec! $init;
assert_eq!(*vec, *vec! $init);
check_eq(&*vec, &*vec! $init);
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/schemars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![cfg(feature = "schemars")]
use {
schemars::schema_for,
smallvec::SmallVec
};

#[test]
fn schema_matches_vec() {
assert_eq!(schema_for!(SmallVec<u32, 4>), schema_for!(Vec<u32>));
}
Loading