From 1aaa162e0bb6e9d6b8bd4f5d5c222eb65b3f62c6 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sun, 26 May 2024 15:20:56 +0100 Subject: [PATCH] Make `schema_name()` return `Cow<'static, str>` instead of `String` --- docs/2-implementing.md | 13 +++---- schemars/src/gen.rs | 35 +++++++++---------- schemars/src/json_schema_impls/array.rs | 13 ++++--- schemars/src/json_schema_impls/arrayvec07.rs | 4 +-- schemars/src/json_schema_impls/chrono04.rs | 12 +++---- schemars/src/json_schema_impls/core.rs | 24 ++++++------- schemars/src/json_schema_impls/decimal.rs | 8 ++--- schemars/src/json_schema_impls/either1.rs | 10 ++---- schemars/src/json_schema_impls/ffi.rs | 6 ++-- schemars/src/json_schema_impls/maps.rs | 6 ++-- schemars/src/json_schema_impls/mod.rs | 2 +- .../src/json_schema_impls/nonzero_signed.rs | 6 ++-- .../src/json_schema_impls/nonzero_unsigned.rs | 8 ++--- schemars/src/json_schema_impls/primitives.rs | 30 +++++----------- schemars/src/json_schema_impls/semver1.rs | 6 ++-- schemars/src/json_schema_impls/sequences.rs | 14 ++++---- schemars/src/json_schema_impls/serdejson.rs | 16 +++------ schemars/src/json_schema_impls/time.rs | 12 +++---- schemars/src/json_schema_impls/tuple.rs | 6 ++-- schemars/src/json_schema_impls/url2.rs | 6 ++-- schemars/src/json_schema_impls/uuid1.rs | 6 ++-- schemars/src/lib.rs | 18 +++++----- schemars/src/schema.rs | 4 +-- schemars_derive/src/lib.rs | 14 +++++--- schemars_derive/src/schema_exprs.rs | 4 +-- 25 files changed, 126 insertions(+), 157 deletions(-) diff --git a/docs/2-implementing.md b/docs/2-implementing.md index c8dfb6e..b3cea57 100644 --- a/docs/2-implementing.md +++ b/docs/2-implementing.md @@ -12,13 +12,11 @@ permalink: /implementing/ ## schema_name ```rust -fn schema_name() -> String; +fn schema_name() -> Cow<'static, str>; ``` This function returns the human-readable friendly name of the type's schema, which frequently is just the name of the type itself. The schema name is used as the title for root schemas, and the key within the root's `definitions` property for subschemas. -NB in a future version of schemars, it's likely that this function will be changed to return a `Cow<'static, str>`. - ## schema_id ```rust @@ -29,8 +27,7 @@ This function returns a unique identifier of the type's schema - if two types re ```rust fn schema_id() -> Cow<'static, str> { - Cow::Owned( - format!("[{}]", T::schema_id())) + format!("[{}]", T::schema_id()).into() } ``` @@ -40,14 +37,14 @@ For a type with no generic type arguments, a reasonable implementation of this f ```rust impl JsonSchema for NonGenericType { - fn schema_name() -> String { + fn schema_name() -> Cow<'static, str> { // Exclude the module path to make the name in generated schemas clearer. - "NonGenericType".to_owned() + "NonGenericType".into() } fn schema_id() -> Cow<'static, str> { // Include the module, in case a type with the same name is in another module/crate - Cow::Borrowed(concat!(module_path!(), "::NonGenericType")) + concat!(module_path!(), "::NonGenericType").into() } fn json_schema(_gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/gen.rs b/schemars/src/gen.rs index 038228c..0bb1053 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/gen.rs @@ -12,10 +12,11 @@ use crate::{visit::*, JsonSchema}; use dyn_clone::DynClone; use serde::Serialize; use serde_json::{Map, Value}; -use std::borrow::Cow; use std::collections::HashMap; use std::{any::Any, collections::HashSet, fmt::Debug}; +type CowStr = std::borrow::Cow<'static, str>; + /// Settings to customize how Schemas are generated. /// /// The default settings currently conform to [JSON Schema 2020-12](https://json-schema.org/specification-links#2020-12), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added. @@ -166,9 +167,9 @@ impl SchemaSettings { pub struct SchemaGenerator { settings: SchemaSettings, definitions: Map, - pending_schema_ids: HashSet>, - schema_id_to_name: HashMap, String>, - used_schema_names: HashSet, + pending_schema_ids: HashSet, + schema_id_to_name: HashMap, + used_schema_names: HashSet, } impl Clone for SchemaGenerator { @@ -230,11 +231,11 @@ impl SchemaGenerator { Some(n) => n, None => { let base_name = T::schema_name(); - let mut name = String::new(); + let mut name = CowStr::Borrowed(""); - if self.used_schema_names.contains(&base_name) { + if self.used_schema_names.contains(base_name.as_ref()) { for i in 2.. { - name = format!("{}{}", base_name, i); + name = format!("{}{}", base_name, i).into(); if !self.used_schema_names.contains(&name) { break; } @@ -250,7 +251,7 @@ impl SchemaGenerator { }; let reference = format!("#{}/{}", self.definitions_path_stripped(), name); - if !self.definitions.contains_key(&name) { + if !self.definitions.contains_key(name.as_ref()) { self.insert_new_subschema_for::(name, id); } Schema::new_ref(reference) @@ -259,18 +260,14 @@ impl SchemaGenerator { } } - fn insert_new_subschema_for( - &mut self, - name: String, - id: Cow<'static, str>, - ) { + fn insert_new_subschema_for(&mut self, name: CowStr, id: CowStr) { let dummy = false.into(); // insert into definitions BEFORE calling json_schema to avoid infinite recursion - self.definitions.insert(name.clone(), dummy); + self.definitions.insert(name.clone().into(), dummy); let schema = self.json_schema_internal::(id); - self.definitions.insert(name, schema.to_value()); + self.definitions.insert(name.into(), schema.to_value()); } /// Borrows the collection of all [referenceable](JsonSchema::is_referenceable) schemas that have been generated. @@ -410,15 +407,15 @@ impl SchemaGenerator { Ok(schema) } - fn json_schema_internal(&mut self, id: Cow<'static, str>) -> Schema { + fn json_schema_internal(&mut self, id: CowStr) -> Schema { struct PendingSchemaState<'a> { gen: &'a mut SchemaGenerator, - id: Cow<'static, str>, + id: CowStr, did_add: bool, } impl<'a> PendingSchemaState<'a> { - fn new(gen: &'a mut SchemaGenerator, id: Cow<'static, str>) -> Self { + fn new(gen: &'a mut SchemaGenerator, id: CowStr) -> Self { let did_add = gen.pending_schema_ids.insert(id.clone()); Self { gen, id, did_add } } @@ -477,6 +474,8 @@ impl SchemaGenerator { } } + /// Returns `self.settings.definitions_path` as a plain JSON pointer to the definitions object, + /// i.e. without a leading '#' or trailing '/' fn definitions_path_stripped(&self) -> &str { let path = &self.settings.definitions_path; let path = path.strip_prefix('#').unwrap_or(path); diff --git a/schemars/src/json_schema_impls/array.rs b/schemars/src/json_schema_impls/array.rs index bacc079..1da1abb 100644 --- a/schemars/src/json_schema_impls/array.rs +++ b/schemars/src/json_schema_impls/array.rs @@ -6,12 +6,12 @@ use std::borrow::Cow; impl JsonSchema for [T; 0] { no_ref_schema!(); - fn schema_name() -> String { - "EmptyArray".to_owned() + fn schema_name() -> Cow<'static, str> { + "EmptyArray".into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("[]") + "[]".into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { @@ -28,13 +28,12 @@ macro_rules! array_impls { impl JsonSchema for [T; $len] { no_ref_schema!(); - fn schema_name() -> String { - format!("Array_size_{}_of_{}", $len, T::schema_name()) + fn schema_name() -> Cow<'static, str> { + format!("Array_size_{}_of_{}", $len, T::schema_name()).into() } fn schema_id() -> Cow<'static, str> { - Cow::Owned( - format!("[{}; {}]", $len, T::schema_id())) + format!("[{}; {}]", $len, T::schema_id()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/arrayvec07.rs b/schemars/src/json_schema_impls/arrayvec07.rs index 3d4d050..920f7c3 100644 --- a/schemars/src/json_schema_impls/arrayvec07.rs +++ b/schemars/src/json_schema_impls/arrayvec07.rs @@ -12,8 +12,8 @@ where { no_ref_schema!(); - fn schema_name() -> String { - format!("Array_up_to_size_{}_of_{}", CAP, T::schema_name()) + fn schema_name() -> std::borrow::Cow<'static, str> { + format!("Array_up_to_size_{}_of_{}", CAP, T::schema_name()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/chrono04.rs b/schemars/src/json_schema_impls/chrono04.rs index 5fd23e8..e8afa2a 100644 --- a/schemars/src/json_schema_impls/chrono04.rs +++ b/schemars/src/json_schema_impls/chrono04.rs @@ -6,12 +6,12 @@ use std::borrow::Cow; impl JsonSchema for Weekday { no_ref_schema!(); - fn schema_name() -> String { - "Weekday".to_owned() + fn schema_name() -> Cow<'static, str> { + "Weekday".into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("chrono::Weekday") + "chrono::Weekday".into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { @@ -38,12 +38,12 @@ macro_rules! formatted_string_impl { impl $($desc)+ { no_ref_schema!(); - fn schema_name() -> String { - stringify!($ty).to_owned() + fn schema_name() -> Cow<'static, str> { + stringify!($ty).into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed(stringify!(chrono::$ty)) + stringify!(chrono::$ty).into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/core.rs b/schemars/src/json_schema_impls/core.rs index 16104f8..0ca4e10 100644 --- a/schemars/src/json_schema_impls/core.rs +++ b/schemars/src/json_schema_impls/core.rs @@ -7,12 +7,12 @@ use std::ops::{Bound, Range, RangeInclusive}; impl JsonSchema for Option { no_ref_schema!(); - fn schema_name() -> String { - format!("Nullable_{}", T::schema_name()) + fn schema_name() -> Cow<'static, str> { + format!("Nullable_{}", T::schema_name()).into() } fn schema_id() -> Cow<'static, str> { - Cow::Owned(format!("Option<{}>", T::schema_id())) + format!("Option<{}>", T::schema_id()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { @@ -69,12 +69,12 @@ impl JsonSchema for Option { } impl JsonSchema for Result { - fn schema_name() -> String { - format!("Result_of_{}_or_{}", T::schema_name(), E::schema_name()) + fn schema_name() -> Cow<'static, str> { + format!("Result_of_{}_or_{}", T::schema_name(), E::schema_name()).into() } fn schema_id() -> Cow<'static, str> { - Cow::Owned(format!("Result<{}, {}>", T::schema_id(), E::schema_id())) + format!("Result<{}, {}>", T::schema_id(), E::schema_id()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { @@ -100,12 +100,12 @@ impl JsonSchema for Result { } impl JsonSchema for Bound { - fn schema_name() -> String { - format!("Bound_of_{}", T::schema_name()) + fn schema_name() -> Cow<'static, str> { + format!("Bound_of_{}", T::schema_name()).into() } fn schema_id() -> Cow<'static, str> { - Cow::Owned(format!("Bound<{}>", T::schema_id())) + format!("Bound<{}>", T::schema_id()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { @@ -135,12 +135,12 @@ impl JsonSchema for Bound { } impl JsonSchema for Range { - fn schema_name() -> String { - format!("Range_of_{}", T::schema_name()) + fn schema_name() -> Cow<'static, str> { + format!("Range_of_{}", T::schema_name()).into() } fn schema_id() -> Cow<'static, str> { - Cow::Owned(format!("Range<{}>", T::schema_id())) + format!("Range<{}>", T::schema_id()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/decimal.rs b/schemars/src/json_schema_impls/decimal.rs index 462e3d5..0ca9840 100644 --- a/schemars/src/json_schema_impls/decimal.rs +++ b/schemars/src/json_schema_impls/decimal.rs @@ -7,12 +7,8 @@ macro_rules! decimal_impl { impl JsonSchema for $type { no_ref_schema!(); - fn schema_name() -> String { - "Decimal".to_owned() - } - - fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("Decimal") + fn schema_name() -> Cow<'static, str> { + "Decimal".into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/either1.rs b/schemars/src/json_schema_impls/either1.rs index 96ed8a8..334e973 100644 --- a/schemars/src/json_schema_impls/either1.rs +++ b/schemars/src/json_schema_impls/either1.rs @@ -6,16 +6,12 @@ use std::borrow::Cow; impl JsonSchema for Either { no_ref_schema!(); - fn schema_name() -> String { - format!("Either_{}_or_{}", L::schema_name(), R::schema_name()) + fn schema_name() -> Cow<'static, str> { + format!("Either_{}_or_{}", L::schema_name(), R::schema_name()).into() } fn schema_id() -> Cow<'static, str> { - Cow::Owned(format!( - "either::Either<{}, {}>", - L::schema_id(), - R::schema_id() - )) + format!("either::Either<{}, {}>", L::schema_id(), R::schema_id()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/ffi.rs b/schemars/src/json_schema_impls/ffi.rs index 4ad85c2..026d5ae 100644 --- a/schemars/src/json_schema_impls/ffi.rs +++ b/schemars/src/json_schema_impls/ffi.rs @@ -4,12 +4,12 @@ use std::borrow::Cow; use std::ffi::{CStr, CString, OsStr, OsString}; impl JsonSchema for OsString { - fn schema_name() -> String { - "OsString".to_owned() + fn schema_name() -> Cow<'static, str> { + "OsString".into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("std::ffi::OsString") + "std::ffi::OsString".into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/maps.rs b/schemars/src/json_schema_impls/maps.rs index b934f8b..9a630a7 100644 --- a/schemars/src/json_schema_impls/maps.rs +++ b/schemars/src/json_schema_impls/maps.rs @@ -10,12 +10,12 @@ macro_rules! map_impl { { no_ref_schema!(); - fn schema_name() -> String { - format!("Map_of_{}", V::schema_name()) + fn schema_name() -> Cow<'static, str> { + format!("Map_of_{}", V::schema_name()).into() } fn schema_id() -> Cow<'static, str> { - Cow::Owned(format!("Map<{}>", V::schema_id())) + format!("Map<{}>", V::schema_id()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index ebcb61b..e45e84c 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -13,7 +13,7 @@ macro_rules! forward_impl { <$target>::is_referenceable() } - fn schema_name() -> String { + fn schema_name() -> std::borrow::Cow<'static, str> { <$target>::schema_name() } diff --git a/schemars/src/json_schema_impls/nonzero_signed.rs b/schemars/src/json_schema_impls/nonzero_signed.rs index d1b5142..634acc1 100644 --- a/schemars/src/json_schema_impls/nonzero_signed.rs +++ b/schemars/src/json_schema_impls/nonzero_signed.rs @@ -8,12 +8,12 @@ macro_rules! nonzero_unsigned_impl { impl JsonSchema for $type { no_ref_schema!(); - fn schema_name() -> String { - stringify!($type).to_owned() + fn schema_name() -> Cow<'static, str> { + stringify!($type).into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed(stringify!(std::num::$type)) + stringify!(std::num::$type).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/nonzero_unsigned.rs b/schemars/src/json_schema_impls/nonzero_unsigned.rs index e3c6d1d..743af65 100644 --- a/schemars/src/json_schema_impls/nonzero_unsigned.rs +++ b/schemars/src/json_schema_impls/nonzero_unsigned.rs @@ -1,6 +1,6 @@ use crate::gen::SchemaGenerator; -use crate::Schema; use crate::JsonSchema; +use crate::Schema; use std::borrow::Cow; use std::num::*; @@ -9,12 +9,12 @@ macro_rules! nonzero_unsigned_impl { impl JsonSchema for $type { no_ref_schema!(); - fn schema_name() -> String { - stringify!($type).to_owned() + fn schema_name() -> Cow<'static, str> { + stringify!($type).into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed(stringify!(std::num::$type)) + stringify!(std::num::$type).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/primitives.rs b/schemars/src/json_schema_impls/primitives.rs index 63121fe..b65a93a 100644 --- a/schemars/src/json_schema_impls/primitives.rs +++ b/schemars/src/json_schema_impls/primitives.rs @@ -9,12 +9,8 @@ macro_rules! simple_impl { impl JsonSchema for $type { no_ref_schema!(); - fn schema_name() -> String { - $instance_type.to_owned() - } - - fn schema_id() -> Cow<'static, str> { - Cow::Borrowed($instance_type) + fn schema_name() -> Cow<'static, str> { + $instance_type.into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { @@ -28,12 +24,8 @@ macro_rules! simple_impl { impl JsonSchema for $type { no_ref_schema!(); - fn schema_name() -> String { - $format.to_owned() - } - - fn schema_id() -> Cow<'static, str> { - Cow::Borrowed($format) + fn schema_name() -> Cow<'static, str> { + $format.into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { @@ -75,12 +67,8 @@ macro_rules! unsigned_impl { impl JsonSchema for $type { no_ref_schema!(); - fn schema_name() -> String { - $format.to_owned() - } - - fn schema_id() -> Cow<'static, str> { - Cow::Borrowed($format) + fn schema_name() -> Cow<'static, str> { + $format.into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { @@ -104,12 +92,12 @@ unsigned_impl!(usize => "integer", "uint"); impl JsonSchema for char { no_ref_schema!(); - fn schema_name() -> String { - "Character".to_owned() + fn schema_name() -> Cow<'static, str> { + "Character".into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("char") + "char".into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/semver1.rs b/schemars/src/json_schema_impls/semver1.rs index fb47f78..dbcdf21 100644 --- a/schemars/src/json_schema_impls/semver1.rs +++ b/schemars/src/json_schema_impls/semver1.rs @@ -6,12 +6,12 @@ use std::borrow::Cow; impl JsonSchema for Version { no_ref_schema!(); - fn schema_name() -> String { - "Version".to_owned() + fn schema_name() -> Cow<'static, str> { + "Version".into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("semver::Version") + "semver::Version".into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/sequences.rs b/schemars/src/json_schema_impls/sequences.rs index d14d4d3..dcc680f 100644 --- a/schemars/src/json_schema_impls/sequences.rs +++ b/schemars/src/json_schema_impls/sequences.rs @@ -10,13 +10,12 @@ macro_rules! seq_impl { { no_ref_schema!(); - fn schema_name() -> String { - format!("Array_of_{}", T::schema_name()) + fn schema_name() -> Cow<'static, str> { + format!("Array_of_{}", T::schema_name()).into() } fn schema_id() -> Cow<'static, str> { - Cow::Owned( - format!("[{}]", T::schema_id())) + format!("[{}]", T::schema_id()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { @@ -37,13 +36,12 @@ macro_rules! set_impl { { no_ref_schema!(); - fn schema_name() -> String { - format!("Set_of_{}", T::schema_name()) + fn schema_name() -> Cow<'static, str> { + format!("Set_of_{}", T::schema_name()).into() } fn schema_id() -> Cow<'static, str> { - Cow::Owned( - format!("Set<{}>", T::schema_id())) + format!("Set<{}>", T::schema_id()).into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/serdejson.rs b/schemars/src/json_schema_impls/serdejson.rs index 5c6b58d..fe76748 100644 --- a/schemars/src/json_schema_impls/serdejson.rs +++ b/schemars/src/json_schema_impls/serdejson.rs @@ -7,12 +7,8 @@ use std::collections::BTreeMap; impl JsonSchema for Value { no_ref_schema!(); - fn schema_name() -> String { - "AnyValue".to_owned() - } - - fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("AnyValue") + fn schema_name() -> Cow<'static, str> { + "AnyValue".into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { @@ -25,12 +21,8 @@ forward_impl!(Map => BTreeMap); impl JsonSchema for Number { no_ref_schema!(); - fn schema_name() -> String { - "Number".to_owned() - } - - fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("Number") + fn schema_name() -> Cow<'static, str> { + "Number".into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/time.rs b/schemars/src/json_schema_impls/time.rs index 7317cfd..0f1eb74 100644 --- a/schemars/src/json_schema_impls/time.rs +++ b/schemars/src/json_schema_impls/time.rs @@ -4,12 +4,12 @@ use std::borrow::Cow; use std::time::{Duration, SystemTime}; impl JsonSchema for Duration { - fn schema_name() -> String { - "Duration".to_owned() + fn schema_name() -> Cow<'static, str> { + "Duration".into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("std::time::Duration") + "std::time::Duration".into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { @@ -25,12 +25,12 @@ impl JsonSchema for Duration { } impl JsonSchema for SystemTime { - fn schema_name() -> String { - "SystemTime".to_owned() + fn schema_name() -> Cow<'static, str> { + "SystemTime".into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("std::time::SystemTime") + "std::time::SystemTime".into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/tuple.rs b/schemars/src/json_schema_impls/tuple.rs index 204169e..2af816a 100644 --- a/schemars/src/json_schema_impls/tuple.rs +++ b/schemars/src/json_schema_impls/tuple.rs @@ -8,10 +8,10 @@ macro_rules! tuple_impls { impl<$($name: JsonSchema),+> JsonSchema for ($($name,)+) { no_ref_schema!(); - fn schema_name() -> String { + fn schema_name() -> Cow<'static, str> { let mut name = "Tuple_of_".to_owned(); name.push_str(&[$($name::schema_name()),+].join("_and_")); - name + name.into() } fn schema_id() -> Cow<'static, str> { @@ -19,7 +19,7 @@ macro_rules! tuple_impls { id.push_str(&[$($name::schema_id()),+].join(",")); id.push(')'); - Cow::Owned(id) + id.into() } fn json_schema(gen: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/url2.rs b/schemars/src/json_schema_impls/url2.rs index 2fcfd04..04fa1e8 100644 --- a/schemars/src/json_schema_impls/url2.rs +++ b/schemars/src/json_schema_impls/url2.rs @@ -6,12 +6,12 @@ use url2::Url; impl JsonSchema for Url { no_ref_schema!(); - fn schema_name() -> String { - "Url".to_owned() + fn schema_name() -> Cow<'static, str> { + "Url".into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("url::Url") + "url::Url".into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/json_schema_impls/uuid1.rs b/schemars/src/json_schema_impls/uuid1.rs index 825f7a2..297914e 100644 --- a/schemars/src/json_schema_impls/uuid1.rs +++ b/schemars/src/json_schema_impls/uuid1.rs @@ -6,12 +6,12 @@ use uuid1::Uuid; impl JsonSchema for Uuid { no_ref_schema!(); - fn schema_name() -> String { - "Uuid".to_string() + fn schema_name() -> Cow<'static, str> { + "Uuid".into() } fn schema_id() -> Cow<'static, str> { - Cow::Borrowed("uuid::Uuid") + "uuid::Uuid".into() } fn json_schema(_: &mut SchemaGenerator) -> Schema { diff --git a/schemars/src/lib.rs b/schemars/src/lib.rs index 32bb8c0..b45fb21 100644 --- a/schemars/src/lib.rs +++ b/schemars/src/lib.rs @@ -56,14 +56,14 @@ pub use schema::Schema; /// struct NonGenericType; /// /// impl JsonSchema for NonGenericType { -/// fn schema_name() -> String { +/// fn schema_name() -> Cow<'static, str> { /// // Exclude the module path to make the name in generated schemas clearer. -/// "NonGenericType".to_owned() +/// "NonGenericType".into() /// } /// /// fn schema_id() -> Cow<'static, str> { /// // Include the module, in case a type with the same name is in another module/crate -/// Cow::Borrowed(concat!(module_path!(), "::NonGenericType")) +/// concat!(module_path!(), "::NonGenericType").into() /// } /// /// fn json_schema(_gen: &mut SchemaGenerator) -> Schema { @@ -82,16 +82,16 @@ pub use schema::Schema; /// struct GenericType(PhantomData); /// /// impl JsonSchema for GenericType { -/// fn schema_name() -> String { -/// format!("GenericType_{}", T::schema_name()) +/// fn schema_name() -> Cow<'static, str> { +/// format!("GenericType_{}", T::schema_name()).into() /// } /// /// fn schema_id() -> Cow<'static, str> { -/// Cow::Owned(format!( +/// format!( /// "{}::GenericType<{}>", /// module_path!(), /// T::schema_id() -/// )) +/// ).into() /// } /// /// fn json_schema(_gen: &mut SchemaGenerator) -> Schema { @@ -117,7 +117,7 @@ pub trait JsonSchema { /// The name of the generated JSON Schema. /// /// This is used as the title for root schemas, and the key within the root's `definitions` property for subschemas. - fn schema_name() -> String; + fn schema_name() -> Cow<'static, str>; /// Returns a string that uniquely identifies the schema produced by this type. /// @@ -127,7 +127,7 @@ pub trait JsonSchema { /// /// The default implementation returns the same value as `schema_name()`. fn schema_id() -> Cow<'static, str> { - Cow::Owned(Self::schema_name()) + Self::schema_name() } /// Generates a JSON Schema for this type. diff --git a/schemars/src/schema.rs b/schemars/src/schema.rs index 6eb55e8..91a3547 100644 --- a/schemars/src/schema.rs +++ b/schemars/src/schema.rs @@ -175,8 +175,8 @@ impl From for Schema { } impl crate::JsonSchema for Schema { - fn schema_name() -> String { - "Schema".to_owned() + fn schema_name() -> std::borrow::Cow<'static, str> { + "Schema".into() } fn schema_id() -> std::borrow::Cow<'static, str> { diff --git a/schemars_derive/src/lib.rs b/schemars_derive/src/lib.rs index c890f75..a4c0742 100644 --- a/schemars_derive/src/lib.rs +++ b/schemars_derive/src/lib.rs @@ -60,7 +60,7 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result::is_referenceable() } - fn schema_name() -> std::string::String { + fn schema_name() -> std::borrow::Cow<'static, str> { <#ty as schemars::JsonSchema>::schema_name() } @@ -104,7 +104,7 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result syn::Result syn::Result syn::Result std::string::String { + fn schema_name() -> std::borrow::Cow<'static, str> { #schema_name } diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index 87e789e..9fbc9ee 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -114,8 +114,8 @@ fn type_for_schema(with_attr: &WithAttr) -> (syn::Type, Option) { false } - fn schema_name() -> std::string::String { - #fn_name.to_string() + fn schema_name() -> std::borrow::Cow<'static, str> { + std::borrow::Cow::Borrowed(#fn_name) } fn schema_id() -> std::borrow::Cow<'static, str> {