From 475a751b7079b3539986b10498ba721bacd0b610 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Fri, 5 Jun 2020 23:48:03 +0100 Subject: [PATCH] Require Visitors to implement Clone --- CHANGELOG.md | 1 + schemars/Cargo.toml | 1 + schemars/src/gen.rs | 30 ++++++++---------------------- schemars/src/visit.rs | 5 ++++- 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b38814..4e35cb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Removed (**BREAKING CHANGES**): - `SchemaSettings::bool_schemas` - this has been superseded by the `ReplaceBoolSchemas` visitor - `SchemaSettings::allow_ref_siblings` - this has been superseded by the `RemoveRefSiblings` visitor +- `SchemaSettings` no longer implements `PartialEq` ### Fixed: - **BREAKING CHANGE** unknown items in `#[schemars(...)]` attributes now cause a compilation error (https://github.com/GREsau/schemars/issues/18) diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index 3d7e980..7d84eb6 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -16,6 +16,7 @@ build = "build.rs" schemars_derive = { version = "=0.7.6", optional = true, path = "../schemars_derive" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +dyn-clone = "1.0" chrono = { version = "0.4", default-features = false, optional = true } indexmap = { version = "1.2", optional = true } diff --git a/schemars/src/gen.rs b/schemars/src/gen.rs index b3f02a9..ecdc923 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/gen.rs @@ -1,13 +1,12 @@ use crate::flatten::Merge; use crate::schema::*; use crate::{visit::*, JsonSchema, Map}; -use std::sync::Arc; /// Settings to customize how Schemas are generated. /// /// The default settings currently conform to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added. /// If you require your generated schemas to conform to draft 7, consider using the [`draft07`](#method.draft07) method. -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone)] pub struct SchemaSettings { /// If `true`, schemas for [`Option`](Option) will include a `nullable` property. /// @@ -46,7 +45,7 @@ impl SchemaSettings { option_add_null_type: true, definitions_path: "#/definitions/".to_owned(), meta_schema: Some("http://json-schema.org/draft-07/schema#".to_owned()), - visitors: Visitors(vec![Arc::new(RemoveRefSiblings)]), + visitors: Visitors(vec![Box::new(RemoveRefSiblings)]), _hidden: (), } } @@ -74,8 +73,8 @@ impl SchemaSettings { .to_owned(), ), visitors: Visitors(vec![ - Arc::new(RemoveRefSiblings), - Arc::new(ReplaceBoolSchemas { + Box::new(RemoveRefSiblings), + Box::new(ReplaceBoolSchemas { skip_additional_properties: true, }), ]), @@ -102,7 +101,7 @@ impl SchemaSettings { /// TODO document pub fn with_visitor(mut self, visitor: impl Visitor + 'static) -> Self { - self.visitors.0.push(Arc::new(visitor)); + self.visitors.0.push(Box::new(visitor)); self } @@ -114,20 +113,7 @@ impl SchemaSettings { /// TODO document #[derive(Debug, Clone, Default)] -pub struct Visitors(Vec>); - -impl PartialEq for Visitors { - fn eq(&self, other: &Self) -> bool { - if self.0.len() != other.0.len() { - return false; - } - - self.0 - .iter() - .zip(other.0.iter()) - .all(|(a, b)| Arc::ptr_eq(a, b)) - } -} +pub struct Visitors(Vec>); /// The main type used to generate JSON Schemas. /// @@ -338,7 +324,7 @@ impl SchemaGenerator { } /// TODO document -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ReplaceBoolSchemas { pub skip_additional_properties: bool, } @@ -375,7 +361,7 @@ impl Visitor for ReplaceBoolSchemas { } /// TODO document -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct RemoveRefSiblings; impl Visitor for RemoveRefSiblings { diff --git a/schemars/src/visit.rs b/schemars/src/visit.rs index 2f7b454..8a2c902 100644 --- a/schemars/src/visit.rs +++ b/schemars/src/visit.rs @@ -1,7 +1,8 @@ use crate::schema::{RootSchema, Schema, SchemaObject, SingleOrVec}; +use dyn_clone::DynClone; use std::fmt::Debug; -pub trait Visitor: Debug { +pub trait Visitor: Debug + DynClone { fn visit_root_schema(&self, root: &mut RootSchema) { visit_root_schema(self, root) } @@ -15,6 +16,8 @@ pub trait Visitor: Debug { } } +dyn_clone::clone_trait_object!(Visitor); + pub fn visit_root_schema(v: &V, root: &mut RootSchema) { v.visit_schema_object(&mut root.schema); visit_map_values(v, &mut root.definitions);