From 75305684984684edf612b89d13d09a143ae44fb4 Mon Sep 17 00:00:00 2001 From: Piotr Siuszko Date: Mon, 23 Jun 2025 18:06:11 +0200 Subject: [PATCH] Clippy fixes --- crates/bevy_remote/src/builtin_methods.rs | 2 +- crates/bevy_remote/src/schemas/json_schema.rs | 2 +- crates/bevy_remote/src/schemas/mod.rs | 2 +- .../bevy_remote/src/schemas/reflect_info.rs | 109 ++++++++---------- 4 files changed, 54 insertions(+), 61 deletions(-) diff --git a/crates/bevy_remote/src/builtin_methods.rs b/crates/bevy_remote/src/builtin_methods.rs index 8d78182908..9ad945a4f7 100644 --- a/crates/bevy_remote/src/builtin_methods.rs +++ b/crates/bevy_remote/src/builtin_methods.rs @@ -1,7 +1,7 @@ //! Built-in verbs for the Bevy Remote Protocol. use core::any::TypeId; -use std::borrow::Cow; +use alloc::borrow::Cow; use anyhow::{anyhow, Result as AnyhowResult}; use bevy_ecs::{ diff --git a/crates/bevy_remote/src/schemas/json_schema.rs b/crates/bevy_remote/src/schemas/json_schema.rs index 8657d4e8d4..b17674230b 100644 --- a/crates/bevy_remote/src/schemas/json_schema.rs +++ b/crates/bevy_remote/src/schemas/json_schema.rs @@ -8,7 +8,7 @@ use bevy_reflect::{ use core::any::TypeId; use serde::{Deserialize, Serialize}; use serde_json::Value; -use std::borrow::Cow; +use alloc::borrow::Cow; use crate::schemas::{ reflect_info::{SchemaInfoReflect, SchemaNumber}, diff --git a/crates/bevy_remote/src/schemas/mod.rs b/crates/bevy_remote/src/schemas/mod.rs index 1d5afdbb49..c7d97c8558 100644 --- a/crates/bevy_remote/src/schemas/mod.rs +++ b/crates/bevy_remote/src/schemas/mod.rs @@ -10,7 +10,7 @@ use bevy_reflect::{ TypeRegistration, }; use core::any::TypeId; -use std::borrow::Cow; +use alloc::borrow::Cow; use crate::schemas::json_schema::JsonSchemaBevyType; diff --git a/crates/bevy_remote/src/schemas/reflect_info.rs b/crates/bevy_remote/src/schemas/reflect_info.rs index 148f9e7fda..6a67de8d9b 100644 --- a/crates/bevy_remote/src/schemas/reflect_info.rs +++ b/crates/bevy_remote/src/schemas/reflect_info.rs @@ -2,7 +2,7 @@ use bevy_reflect::{GenericInfo, NamedField, Reflect, TypeInfo, UnnamedField, VariantInfo}; use core::any::TypeId; use serde::{Deserialize, Serialize}; -use std::{ +use core::{ any::Any, fmt::Debug, ops::{Bound, RangeBounds}, @@ -148,7 +148,7 @@ impl MinMaxValues { } true } - /// Creates MinMaxValues from a reflected range type. + /// Creates `MinMaxValues` from a reflected range type. /// Attempts to downcast the reflected value to the specified range type T /// and extract its bounds. pub fn from_reflect(reflect_val: &dyn Reflect) -> Option @@ -165,8 +165,8 @@ impl MinMaxValues { ))) } - /// Creates MinMaxValues from range bounds and a type identifier. - /// Takes a tuple containing start bound, end bound, and TypeId to construct + /// Creates `MinMaxValues` from range bounds and a type identifier. + /// Takes a tuple containing start bound, end bound, and `TypeId` to construct /// the appropriate range constraints. pub fn from_range(value: (Bound<&T>, Bound<&T>, TypeId)) -> MinMaxValues where @@ -226,7 +226,7 @@ impl From for MinMaxValues { pub enum InternalSchemaType { /// Represents array-like types (Vec, arrays, lists, sets). Array { - /// The TypeId of the element type contained in the array. + /// The `TypeId` of the element type contained in the array. element_type: TypeId, /// Optional type information for the element type. element_type_info: Option, @@ -248,13 +248,13 @@ pub enum InternalSchemaType { /// Generic information about the wrapped type T in Option. generic: GenericInfo, }, - /// Represents a Map type (e.g., HashMap). + /// Represents a Map type (e.g., `HashMap`). Map { - /// The TypeId of the key type contained in the map. + /// The `TypeId` of the key type contained in the map. key_type: TypeId, /// Optional type information for the key type. key_type_info: Option, - /// The TypeId of the value type contained in the map. + /// The `TypeId` of the value type contained in the map. value_type: TypeId, /// Optional type information for the value type. value_type_info: Option, @@ -297,10 +297,7 @@ impl From<&SchemaTypeInfo> for Option { SchemaType::Null, ])) } - InternalSchemaType::Regular => match &value.type_id { - Some(s) => Some(SchemaTypeVariant::Single(s.clone().into())), - _ => None, - }, + InternalSchemaType::Regular => value.type_id.as_ref().map(|s| SchemaTypeVariant::Single((*s).into())), } } } @@ -318,45 +315,44 @@ pub struct SchemaTypeInfo { pub kind: SchemaKind, /// Optional Bevy reflection type information. pub type_info: Option, - /// Optional TypeId for the type. + /// Optional `TypeId` for the type. pub type_id: Option, /// Numeric range constraints for the type. pub range: MinMaxValues, } -impl Into for SchemaTypeInfo { - fn into(self) -> JsonSchemaVariant { - let schema_type: Option = (&self).into(); +impl From for JsonSchemaVariant { + fn from(val: SchemaTypeInfo) -> Self { + let schema_type: Option = (&val).into(); let mut schema = JsonSchemaBevyType { schema_type: schema_type.clone(), - kind: self.kind.clone(), - description: self.documentation.clone(), - type_path: self + kind: val.kind.clone(), + description: val.documentation.clone(), + type_path: val .type_info .as_ref() - .and_then(|s| Some(s.type_path_table().path().into())) + .map(|s| (s.type_path_table().path().into())) .unwrap_or_default(), - short_path: self + short_path: val .type_info - .as_ref() - .and_then(|s| Some(s.type_path_table().short_path().into())) + .as_ref().map(|s| s.type_path_table().short_path().into()) .unwrap_or_default(), - crate_name: self + crate_name: val .type_info .as_ref() .and_then(|s| s.type_path_table().crate_name().map(Into::into)), - module_path: self + module_path: val .type_info .as_ref() .and_then(|s| s.type_path_table().module_path().map(Into::into)), - minimum: self.range.min.as_ref().and_then(|r| r.get_inclusive()), - maximum: self.range.max.as_ref().and_then(|r| r.get_inclusive()), - exclusive_minimum: self.range.min.as_ref().and_then(|r| r.get_exclusive()), - exclusive_maximum: self.range.max.as_ref().and_then(|r| r.get_exclusive()), + minimum: val.range.min.as_ref().and_then(BoundValue::get_inclusive), + maximum: val.range.max.as_ref().and_then(BoundValue::get_inclusive), + exclusive_minimum: val.range.min.as_ref().and_then(BoundValue::get_exclusive), + exclusive_maximum: val.range.max.as_ref().and_then(BoundValue::get_exclusive), ..Default::default() }; - match self.internal_type { + match val.internal_type { InternalSchemaType::Map { key_type, key_type_info, @@ -378,7 +374,7 @@ impl Into for SchemaTypeInfo { } InternalSchemaType::Regular => {} InternalSchemaType::EnumHolder(variants) => { - schema.one_of = variants.iter().map(|v| v.build_schema()).collect(); + schema.one_of = variants.iter().map(SchemaInfoReflect::build_schema).collect(); } InternalSchemaType::EnumVariant(variant_info) => match &variant_info { VariantInfo::Struct(struct_variant_info) => { @@ -441,12 +437,12 @@ impl Into for SchemaTypeInfo { if let JsonSchemaVariant::Schema(new_schema_type) = new_schema { schema = *new_schema_type; schema.schema_type = schema_type.clone(); - schema.description = self.documentation.clone(); + schema.description = val.documentation.clone(); } else { return new_schema; } } else { - schema.prefix_items = unnamed_fields.iter().map(|s| s.build_schema()).collect(); + schema.prefix_items = unnamed_fields.iter().map(SchemaInfoReflect::build_schema).collect(); schema.min_items = Some(unnamed_fields.len() as u64); schema.max_items = Some(unnamed_fields.len() as u64); } @@ -470,13 +466,13 @@ impl Into for SchemaTypeInfo { if let JsonSchemaVariant::Schema(value) = schema_variant { schema = *value; schema.schema_type = schema_type.clone(); - schema.minimum = self.range.min.as_ref().and_then(|r| r.get_inclusive()); - schema.maximum = self.range.max.as_ref().and_then(|r| r.get_inclusive()); + schema.minimum = val.range.min.as_ref().and_then(BoundValue::get_inclusive); + schema.maximum = val.range.max.as_ref().and_then(BoundValue::get_inclusive); schema.exclusive_minimum = - self.range.min.as_ref().and_then(|r| r.get_exclusive()); + val.range.min.as_ref().and_then(BoundValue::get_exclusive); schema.exclusive_maximum = - self.range.max.as_ref().and_then(|r| r.get_exclusive()); - schema.description = self.documentation; + val.range.max.as_ref().and_then(BoundValue::get_exclusive); + schema.description = val.documentation; schema.kind = SchemaKind::Optional; } else { return schema_variant; @@ -515,21 +511,18 @@ pub trait SchemaInfoReflect { if SchemaType::try_get_primitive_type_from_type_id(self.get_type()).is_some() { return SchemaKind::Value; } - match self.try_get_type_info() { - Some(type_info) => { - return match type_info { - TypeInfo::Struct(_) => SchemaKind::Struct, - TypeInfo::TupleStruct(_) => SchemaKind::TupleStruct, - TypeInfo::Tuple(_) => SchemaKind::Tuple, - TypeInfo::List(_) => SchemaKind::List, - TypeInfo::Array(_) => SchemaKind::Array, - TypeInfo::Map(_) => SchemaKind::Map, - TypeInfo::Set(_) => SchemaKind::Set, - TypeInfo::Enum(_) => SchemaKind::Enum, - TypeInfo::Opaque(_) => SchemaKind::Opaque, - } + if let Some(type_info) = self.try_get_type_info() { + return match type_info { + TypeInfo::Struct(_) => SchemaKind::Struct, + TypeInfo::TupleStruct(_) => SchemaKind::TupleStruct, + TypeInfo::Tuple(_) => SchemaKind::Tuple, + TypeInfo::List(_) => SchemaKind::List, + TypeInfo::Array(_) => SchemaKind::Array, + TypeInfo::Map(_) => SchemaKind::Map, + TypeInfo::Set(_) => SchemaKind::Set, + TypeInfo::Enum(_) => SchemaKind::Enum, + TypeInfo::Opaque(_) => SchemaKind::Opaque, } - None => {} } SchemaKind::Value } @@ -627,7 +620,7 @@ pub trait SchemaInfoReflect { None } - /// Get the underlying TypeId + /// Get the underlying `TypeId` fn get_type(&self) -> TypeId; /// Try to get the attribute by id @@ -635,7 +628,7 @@ pub trait SchemaInfoReflect { None } - /// Creates MinMaxValues from a reflected range type. + /// Creates `MinMaxValues` from a reflected range type. /// Attempts to downcast the reflected value to the specified range type T /// and extract its bounds. fn min_max_from_attribute(&self) -> Option @@ -647,7 +640,7 @@ pub trait SchemaInfoReflect { .and_then(|reflect_value| MinMaxValues::from_reflect::(reflect_value)) } - /// Creates MinMaxValues from a reflected range type. + /// Creates `MinMaxValues` from a reflected range type. /// Attempts to downcast the reflected value to the specified range type T /// and extract its bounds. fn min_max_from_attribute_for_type(&self) -> Option @@ -681,7 +674,7 @@ pub trait SchemaInfoReflect { None } - /// Creates MinMaxValues from a reflected range type. + /// Creates `MinMaxValues` from a reflected range type. /// Attempts to downcast the reflected value to the specified range type T /// and extract its bounds. fn get_range_by_id(&self) -> MinMaxValues { @@ -724,7 +717,7 @@ pub trait SchemaInfoReflect { impl SchemaInfoReflect for UnnamedField { fn try_get_type_info(&self) -> Option { - self.type_info().and_then(|info| Some(info.clone())) + self.type_info().cloned() } #[cfg(feature = "documentation")] fn get_docs(&self) -> Option<&str> { @@ -741,7 +734,7 @@ impl SchemaInfoReflect for UnnamedField { impl SchemaInfoReflect for NamedField { fn try_get_type_info(&self) -> Option { - self.type_info().and_then(|info| Some(info.clone())) + self.type_info().cloned() } #[cfg(feature = "documentation")] fn get_docs(&self) -> Option<&str> {