From 34a4c8ec42fc3d128bac7cc7b88d7c4f840f0dea Mon Sep 17 00:00:00 2001 From: Dennis Hamester Date: Tue, 5 Nov 2024 08:13:49 +0100 Subject: [PATCH] core: Wrap &mut Vec in a References type --- aldrin/src/channel/unbound.rs | 12 ++-- aldrin/src/client.rs | 4 +- aldrin/src/lifetime.rs | 8 +-- core/src/generic_value.rs | 4 +- core/src/ids.rs | 18 +++--- core/src/introspection.rs | 40 ++++++++++-- core/src/introspection/test.rs | 18 +++--- core/src/introspection/type_id.rs | 6 +- core/src/serialized_value.rs | 6 +- core/src/value.rs | 98 ++++++++++++++--------------- macros/src/derive/introspectable.rs | 4 +- macros/src/service.rs | 8 +-- macros/src/service/body.rs | 4 +- 13 files changed, 125 insertions(+), 105 deletions(-) diff --git a/aldrin/src/channel/unbound.rs b/aldrin/src/channel/unbound.rs index 444ed76b..9cfc7a94 100644 --- a/aldrin/src/channel/unbound.rs +++ b/aldrin/src/channel/unbound.rs @@ -1,8 +1,6 @@ use super::{Receiver, Sender, UnclaimedReceiver, UnclaimedSender}; #[cfg(feature = "introspection")] -use crate::core::introspection::{ - BuiltInType, DynIntrospectable, Introspectable, Layout, LexicalId, -}; +use crate::core::introspection::{BuiltInType, Introspectable, Layout, LexicalId, References}; use crate::core::{ AsSerializeArg, ChannelCookie, Deserialize, DeserializeError, Deserializer, Serialize, SerializeError, Serializer, @@ -163,8 +161,8 @@ impl Introspectable for UnboundSender { LexicalId::sender(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -321,7 +319,7 @@ impl Introspectable for UnboundReceiver { LexicalId::receiver(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } diff --git a/aldrin/src/client.rs b/aldrin/src/client.rs index d1baa65d..41507300 100644 --- a/aldrin/src/client.rs +++ b/aldrin/src/client.rs @@ -4,7 +4,7 @@ mod select; use crate::bus_listener::{BusListener, BusListenerHandle}; #[cfg(feature = "introspection")] -use crate::core::introspection::{DynIntrospectable, Introspection}; +use crate::core::introspection::{DynIntrospectable, Introspection, References}; use crate::core::message::{ AbortFunctionCall, AddBusListenerFilter, AddChannelCapacity, BusListenerCurrentFinished, CallFunction, CallFunctionReply, CallFunctionResult, ChannelEndClaimed, ChannelEndClosed, @@ -1833,7 +1833,7 @@ where continue; }; - ty.add_references(&mut types); + ty.add_references(&mut References::new(&mut types)); entry.insert(introspection); } } diff --git a/aldrin/src/lifetime.rs b/aldrin/src/lifetime.rs index 8a4737a9..1193d4fd 100644 --- a/aldrin/src/lifetime.rs +++ b/aldrin/src/lifetime.rs @@ -3,9 +3,7 @@ mod test; use crate::bus_listener::BusListenerEvent; #[cfg(feature = "introspection")] -use crate::core::introspection::{ - BuiltInType, DynIntrospectable, Introspectable, Layout, LexicalId, -}; +use crate::core::introspection::{BuiltInType, Introspectable, Layout, LexicalId, References}; use crate::core::{ AsSerializeArg, BusEvent, BusListenerCookie, BusListenerFilter, BusListenerScope, Deserialize, DeserializeError, Deserializer, ObjectId, ObjectUuid, Serialize, SerializeError, Serializer, @@ -127,7 +125,7 @@ impl Introspectable for LifetimeScope { LifetimeId::lexical_id() } - fn add_references(references: &mut Vec) { + fn add_references(references: &mut References) { LifetimeId::add_references(references) } } @@ -190,7 +188,7 @@ impl Introspectable for LifetimeId { LexicalId::LIFETIME } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl From for LifetimeId { diff --git a/core/src/generic_value.rs b/core/src/generic_value.rs index 4b46d10e..bc713952 100644 --- a/core/src/generic_value.rs +++ b/core/src/generic_value.rs @@ -1,7 +1,7 @@ use crate::error::{DeserializeError, SerializeError}; use crate::ids::{ChannelCookie, ObjectId, ServiceId}; #[cfg(feature = "introspection")] -use crate::introspection::{BuiltInType, DynIntrospectable, Introspectable, Layout, LexicalId}; +use crate::introspection::{BuiltInType, Introspectable, Layout, LexicalId, References}; use crate::value::ValueKind; use crate::value_deserializer::{Deserialize, Deserializer}; use crate::value_serializer::{AsSerializeArg, Serialize, Serializer}; @@ -264,7 +264,7 @@ impl Introspectable for Value { LexicalId::VALUE } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } #[derive(Debug, Clone, PartialEq)] diff --git a/core/src/ids.rs b/core/src/ids.rs index f621891d..9a6bd074 100644 --- a/core/src/ids.rs +++ b/core/src/ids.rs @@ -2,7 +2,7 @@ use crate::deserialize_key::DeserializeKey; use crate::error::{DeserializeError, SerializeError}; #[cfg(feature = "introspection")] use crate::introspection::{ - BuiltInType, DynIntrospectable, Introspectable, KeyType, KeyTypeOf, Layout, LexicalId, + BuiltInType, Introspectable, KeyType, KeyTypeOf, Layout, LexicalId, References, }; use crate::serialize_key::SerializeKey; use crate::value_deserializer::{Deserialize, Deserializer}; @@ -84,7 +84,7 @@ impl Introspectable for ObjectId { LexicalId::OBJECT_ID } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } /// UUID of an object. @@ -158,7 +158,7 @@ impl Introspectable for ObjectUuid { LexicalId::UUID } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl SerializeKey for ObjectUuid { @@ -280,7 +280,7 @@ impl Introspectable for ObjectCookie { LexicalId::UUID } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl SerializeKey for ObjectCookie { @@ -403,7 +403,7 @@ impl Introspectable for ServiceId { LexicalId::SERVICE_ID } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } /// UUID of a service. @@ -477,7 +477,7 @@ impl Introspectable for ServiceUuid { LexicalId::UUID } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl SerializeKey for ServiceUuid { @@ -599,7 +599,7 @@ impl Introspectable for ServiceCookie { LexicalId::UUID } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl SerializeKey for ServiceCookie { @@ -711,7 +711,7 @@ impl Introspectable for ChannelCookie { LexicalId::UUID } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl SerializeKey for ChannelCookie { @@ -862,7 +862,7 @@ impl Introspectable for TypeId { LexicalId::UUID } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl SerializeKey for TypeId { diff --git a/core/src/introspection.rs b/core/src/introspection.rs index 957a90ea..34aa950d 100644 --- a/core/src/introspection.rs +++ b/core/src/introspection.rs @@ -55,7 +55,7 @@ impl Introspection { pub fn from_dyn(ty: DynIntrospectable) -> Self { let mut types = Vec::new(); - ty.add_references(&mut types); + ty.add_references(&mut References::new(&mut types)); let mut references = BTreeMap::new(); for ty in types { @@ -155,17 +155,49 @@ impl Deserialize for Introspection { } } +#[derive(Debug)] +pub struct References<'a> { + inner: &'a mut Vec, +} + +impl<'a> References<'a> { + pub fn new(inner: &'a mut Vec) -> Self { + Self { inner } + } + + pub fn add(&mut self) { + self.add_dyn(DynIntrospectable::new::()); + } + + pub fn add_dyn(&mut self, ty: DynIntrospectable) { + self.inner.push(ty); + } + + pub fn reserve(&mut self, additional: usize) { + self.inner.reserve(additional); + } +} + +impl Extend for References<'_> { + fn extend(&mut self, iter: T) + where + T: IntoIterator, + { + self.inner.extend(iter); + } +} + pub trait Introspectable { fn layout() -> Layout; fn lexical_id() -> LexicalId; - fn add_references(references: &mut Vec); + fn add_references(references: &mut References); } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct DynIntrospectable { layout: fn() -> Layout, lexical_id: fn() -> LexicalId, - add_references: fn(&mut Vec), + add_references: fn(&mut References), } impl DynIntrospectable { @@ -185,7 +217,7 @@ impl DynIntrospectable { (self.lexical_id)() } - pub fn add_references(self, references: &mut Vec) { + pub fn add_references(self, references: &mut References) { (self.add_references)(references) } } diff --git a/core/src/introspection/test.rs b/core/src/introspection/test.rs index 1018986f..d3e95c0b 100644 --- a/core/src/introspection/test.rs +++ b/core/src/introspection/test.rs @@ -1,4 +1,4 @@ -use super::{DynIntrospectable, Introspectable, Introspection, Layout, LexicalId, Struct}; +use super::{Introspectable, Introspection, Layout, LexicalId, References, Struct}; #[test] fn duplicate_lexical_id_good() { @@ -13,9 +13,9 @@ fn duplicate_lexical_id_good() { LexicalId::custom("dup", "Dup") } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::<()>()); - references.push(DynIntrospectable::new::<()>()); + fn add_references(references: &mut References) { + references.add::<()>(); + references.add::<()>(); } } @@ -36,7 +36,7 @@ fn duplicate_lexical_id_bad() { LexicalId::custom("dup", "Bad") } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } struct Bad2; @@ -50,7 +50,7 @@ fn duplicate_lexical_id_bad() { LexicalId::custom("dup", "Bad") } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } struct Dup; @@ -64,9 +64,9 @@ fn duplicate_lexical_id_bad() { LexicalId::custom("dup", "Dup") } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); + references.add::(); } } diff --git a/core/src/introspection/type_id.rs b/core/src/introspection/type_id.rs index 635f708a..c32012fb 100644 --- a/core/src/introspection/type_id.rs +++ b/core/src/introspection/type_id.rs @@ -1,4 +1,4 @@ -use super::{DynIntrospectable, Introspectable, Layout, VERSION}; +use super::{DynIntrospectable, Introspectable, Layout, References, VERSION}; use crate::error::SerializeError; use crate::ids::TypeId; use crate::serialized_value::SerializedValue; @@ -16,11 +16,11 @@ impl TypeId { let mut compute = Compute::new(ty.layout()); let mut references = Vec::new(); - ty.add_references(&mut references); + ty.add_references(&mut References::new(&mut references)); while let Some(ty) = references.pop() { if compute.add(ty.layout()) { - ty.add_references(&mut references); + ty.add_references(&mut References::new(&mut references)); } } diff --git a/core/src/serialized_value.rs b/core/src/serialized_value.rs index da675011..f2a5596f 100644 --- a/core/src/serialized_value.rs +++ b/core/src/serialized_value.rs @@ -3,7 +3,7 @@ mod test; use crate::error::{DeserializeError, SerializeError}; #[cfg(feature = "introspection")] -use crate::introspection::{BuiltInType, DynIntrospectable, Introspectable, Layout, LexicalId}; +use crate::introspection::{BuiltInType, Introspectable, Layout, LexicalId, References}; use crate::value::ValueKind; use crate::value_deserializer::{Deserialize, Deserializer}; use crate::value_serializer::{AsSerializeArg, Serialize, Serializer}; @@ -142,7 +142,7 @@ impl Introspectable for SerializedValue { LexicalId::VALUE } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } #[cfg(feature = "fuzzing")] @@ -257,5 +257,5 @@ impl Introspectable for SerializedValueSlice { LexicalId::VALUE } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } diff --git a/core/src/value.rs b/core/src/value.rs index ba4932b7..7a787262 100644 --- a/core/src/value.rs +++ b/core/src/value.rs @@ -6,7 +6,7 @@ use crate::error::{DeserializeError, SerializeError}; #[cfg(feature = "introspection")] use crate::introspection::{ BuiltInType, DynIntrospectable, Introspectable, KeyTypeOf, Layout, LexicalId, MapType, - ResultType, Struct, + References, ResultType, Struct, }; use crate::serialize_key::SerializeKey; use crate::value_deserializer::{Deserialize, Deserializer}; @@ -169,7 +169,7 @@ impl Introspectable for Bytes { LexicalId::BYTES } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } /// Wrapper for `[u8]` to enable `Serialize` and `Deserialize` specializations. @@ -264,7 +264,7 @@ impl Introspectable for ByteSlice { LexicalId::BYTES } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } /// Empty value that deserializes from everything by skipping over it. @@ -307,8 +307,8 @@ impl Introspectable for &T { LexicalId::box_ty(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -342,8 +342,8 @@ impl Introspectable for &mut T { LexicalId::box_ty(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -383,8 +383,8 @@ impl Introspectable for Box { LexicalId::box_ty(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -421,7 +421,7 @@ impl Introspectable for () { LexicalId::UNIT } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for Option { @@ -465,8 +465,8 @@ impl Introspectable for Option { LexicalId::option(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -504,7 +504,7 @@ impl Introspectable for bool { LexicalId::BOOL } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for u8 { @@ -541,7 +541,7 @@ impl Introspectable for u8 { LexicalId::U8 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for i8 { @@ -578,7 +578,7 @@ impl Introspectable for i8 { LexicalId::I8 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for u16 { @@ -615,7 +615,7 @@ impl Introspectable for u16 { LexicalId::U16 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for i16 { @@ -652,7 +652,7 @@ impl Introspectable for i16 { LexicalId::I16 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for u32 { @@ -689,7 +689,7 @@ impl Introspectable for u32 { LexicalId::U32 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for i32 { @@ -726,7 +726,7 @@ impl Introspectable for i32 { LexicalId::I32 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for u64 { @@ -763,7 +763,7 @@ impl Introspectable for u64 { LexicalId::U64 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for i64 { @@ -800,7 +800,7 @@ impl Introspectable for i64 { LexicalId::I64 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for f32 { @@ -837,7 +837,7 @@ impl Introspectable for f32 { LexicalId::F32 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for f64 { @@ -874,7 +874,7 @@ impl Introspectable for f64 { LexicalId::F64 } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for str { @@ -904,7 +904,7 @@ impl Introspectable for str { LexicalId::STRING } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for String { @@ -940,7 +940,7 @@ impl Introspectable for String { LexicalId::STRING } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for Uuid { @@ -977,7 +977,7 @@ impl Introspectable for Uuid { LexicalId::UUID } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for Vec { @@ -1016,8 +1016,8 @@ impl Introspectable for Vec { LexicalId::vec(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -1057,8 +1057,8 @@ impl Introspectable for VecDeque { LexicalId::vec(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -1098,8 +1098,8 @@ impl Introspectable for LinkedList { LexicalId::vec(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -1133,8 +1133,8 @@ impl Introspectable for [T] { LexicalId::vec(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -1218,8 +1218,8 @@ impl Introspectable for [T; N] { LexicalId::vec(T::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); } } @@ -1258,7 +1258,7 @@ impl Introspectable for bytes::Bytes { LexicalId::BYTES } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for bytes::BytesMut { @@ -1296,7 +1296,7 @@ impl Introspectable for bytes::BytesMut { LexicalId::BYTES } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for HashMap { @@ -1344,8 +1344,8 @@ where LexicalId::map(K::KEY_TYPE, V::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()) + fn add_references(references: &mut References) { + references.add::(); } } @@ -1385,8 +1385,8 @@ impl Introspectable for BTreeMap { LexicalId::map(K::KEY_TYPE, V::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()) + fn add_references(references: &mut References) { + references.add::(); } } @@ -1430,7 +1430,7 @@ impl Introspectable for HashSet { LexicalId::set(T::KEY_TYPE) } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl Serialize for BTreeSet { @@ -1469,7 +1469,7 @@ impl Introspectable for BTreeSet { LexicalId::set(T::KEY_TYPE) } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } impl<'a, T> Serialize for Cow<'a, T> @@ -1524,7 +1524,7 @@ where T::lexical_id() } - fn add_references(_references: &mut Vec) {} + fn add_references(_references: &mut References) {} } #[derive(IntoPrimitive, TryFromPrimitive)] @@ -1588,9 +1588,9 @@ impl Introspectable for Result { LexicalId::result(T::lexical_id(), E::lexical_id()) } - fn add_references(references: &mut Vec) { - references.push(DynIntrospectable::new::()); - references.push(DynIntrospectable::new::()); + fn add_references(references: &mut References) { + references.add::(); + references.add::(); } } @@ -1698,7 +1698,7 @@ macro_rules! tuple_impls { ) } - fn add_references(references: &mut Vec) { + fn add_references(references: &mut References) { let types: [DynIntrospectable; $len] = [ $( DynIntrospectable::new::<$gen>() ),+ ]; diff --git a/macros/src/derive/introspectable.rs b/macros/src/derive/introspectable.rs index d7fb0110..1e7c4ea3 100644 --- a/macros/src/derive/introspectable.rs +++ b/macros/src/derive/introspectable.rs @@ -72,9 +72,7 @@ fn gen_introspectable(input: DeriveInput, options: Options) -> Result, - ) { + fn add_references(references: &mut #krate::introspection::References) { #add_references } } diff --git a/macros/src/service.rs b/macros/src/service.rs index 4a5441a3..84ad8f57 100644 --- a/macros/src/service.rs +++ b/macros/src/service.rs @@ -115,9 +115,7 @@ impl Service { <#introspection as #krate::core::introspection::Introspectable>::lexical_id() } - fn add_references( - references: &mut ::std::vec::Vec<#krate::core::introspection::DynIntrospectable>, - ) { + fn add_references(references: &mut #krate::core::introspection::References) { <#introspection as #krate::core::introspection::Introspectable>::add_references( references, ) @@ -207,9 +205,7 @@ impl Service { <#introspection as #krate::core::introspection::Introspectable>::lexical_id() } - fn add_references( - references: &mut ::std::vec::Vec<#krate::core::introspection::DynIntrospectable>, - ) { + fn add_references(references: &mut #krate::core::introspection::References) { <#introspection as #krate::core::introspection::Introspectable>::add_references( references, ) diff --git a/macros/src/service/body.rs b/macros/src/service/body.rs index 51c3c9a2..ad6c6027 100644 --- a/macros/src/service/body.rs +++ b/macros/src/service/body.rs @@ -360,9 +360,7 @@ impl Body { #krate::core::introspection::LexicalId::service(#schema, #service) } - fn add_references( - references: &mut ::std::vec::Vec<#krate::core::introspection::DynIntrospectable>, - ) { + fn add_references(references: &mut #krate::core::introspection::References) { let types: [#krate::core::introspection::DynIntrospectable; #references_len] = [ #( #krate::core::introspection::DynIntrospectable::new::<#references>(), )* ];