Skip to content

Commit

Permalink
core: Wrap &mut Vec<DynIntrospectable> in a References type
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-hamester committed Nov 15, 2024
1 parent aabae4c commit 34a4c8e
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 105 deletions.
12 changes: 5 additions & 7 deletions aldrin/src/channel/unbound.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -163,8 +161,8 @@ impl<T: Introspectable + ?Sized> Introspectable for UnboundSender<T> {
LexicalId::sender(T::lexical_id())
}

fn add_references(references: &mut Vec<DynIntrospectable>) {
references.push(DynIntrospectable::new::<T>());
fn add_references(references: &mut References) {
references.add::<T>();
}
}

Expand Down Expand Up @@ -321,7 +319,7 @@ impl<T: Introspectable> Introspectable for UnboundReceiver<T> {
LexicalId::receiver(T::lexical_id())
}

fn add_references(references: &mut Vec<DynIntrospectable>) {
references.push(DynIntrospectable::new::<T>());
fn add_references(references: &mut References) {
references.add::<T>();
}
}
4 changes: 2 additions & 2 deletions aldrin/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1833,7 +1833,7 @@ where
continue;
};

ty.add_references(&mut types);
ty.add_references(&mut References::new(&mut types));
entry.insert(introspection);
}
}
Expand Down
8 changes: 3 additions & 5 deletions aldrin/src/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -127,7 +125,7 @@ impl Introspectable for LifetimeScope {
LifetimeId::lexical_id()
}

fn add_references(references: &mut Vec<DynIntrospectable>) {
fn add_references(references: &mut References) {
LifetimeId::add_references(references)
}
}
Expand Down Expand Up @@ -190,7 +188,7 @@ impl Introspectable for LifetimeId {
LexicalId::LIFETIME
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

impl From<ObjectId> for LifetimeId {
Expand Down
4 changes: 2 additions & 2 deletions core/src/generic_value.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -264,7 +264,7 @@ impl Introspectable for Value {
LexicalId::VALUE
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
18 changes: 9 additions & 9 deletions core/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Introspectable for ObjectId {
LexicalId::OBJECT_ID
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

/// UUID of an object.
Expand Down Expand Up @@ -158,7 +158,7 @@ impl Introspectable for ObjectUuid {
LexicalId::UUID
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

impl SerializeKey for ObjectUuid {
Expand Down Expand Up @@ -280,7 +280,7 @@ impl Introspectable for ObjectCookie {
LexicalId::UUID
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

impl SerializeKey for ObjectCookie {
Expand Down Expand Up @@ -403,7 +403,7 @@ impl Introspectable for ServiceId {
LexicalId::SERVICE_ID
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

/// UUID of a service.
Expand Down Expand Up @@ -477,7 +477,7 @@ impl Introspectable for ServiceUuid {
LexicalId::UUID
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

impl SerializeKey for ServiceUuid {
Expand Down Expand Up @@ -599,7 +599,7 @@ impl Introspectable for ServiceCookie {
LexicalId::UUID
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

impl SerializeKey for ServiceCookie {
Expand Down Expand Up @@ -711,7 +711,7 @@ impl Introspectable for ChannelCookie {
LexicalId::UUID
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

impl SerializeKey for ChannelCookie {
Expand Down Expand Up @@ -862,7 +862,7 @@ impl Introspectable for TypeId {
LexicalId::UUID
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

impl SerializeKey for TypeId {
Expand Down
40 changes: 36 additions & 4 deletions core/src/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -155,17 +155,49 @@ impl Deserialize for Introspection {
}
}

#[derive(Debug)]
pub struct References<'a> {
inner: &'a mut Vec<DynIntrospectable>,
}

impl<'a> References<'a> {
pub fn new(inner: &'a mut Vec<DynIntrospectable>) -> Self {
Self { inner }
}

pub fn add<T: Introspectable + ?Sized>(&mut self) {
self.add_dyn(DynIntrospectable::new::<T>());
}

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<DynIntrospectable> for References<'_> {
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = DynIntrospectable>,
{
self.inner.extend(iter);
}
}

pub trait Introspectable {
fn layout() -> Layout;
fn lexical_id() -> LexicalId;
fn add_references(references: &mut Vec<DynIntrospectable>);
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<DynIntrospectable>),
add_references: fn(&mut References),
}

impl DynIntrospectable {
Expand All @@ -185,7 +217,7 @@ impl DynIntrospectable {
(self.lexical_id)()
}

pub fn add_references(self, references: &mut Vec<DynIntrospectable>) {
pub fn add_references(self, references: &mut References) {
(self.add_references)(references)
}
}
18 changes: 9 additions & 9 deletions core/src/introspection/test.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -13,9 +13,9 @@ fn duplicate_lexical_id_good() {
LexicalId::custom("dup", "Dup")
}

fn add_references(references: &mut Vec<DynIntrospectable>) {
references.push(DynIntrospectable::new::<()>());
references.push(DynIntrospectable::new::<()>());
fn add_references(references: &mut References) {
references.add::<()>();
references.add::<()>();
}
}

Expand All @@ -36,7 +36,7 @@ fn duplicate_lexical_id_bad() {
LexicalId::custom("dup", "Bad")
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

struct Bad2;
Expand All @@ -50,7 +50,7 @@ fn duplicate_lexical_id_bad() {
LexicalId::custom("dup", "Bad")
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

struct Dup;
Expand All @@ -64,9 +64,9 @@ fn duplicate_lexical_id_bad() {
LexicalId::custom("dup", "Dup")
}

fn add_references(references: &mut Vec<DynIntrospectable>) {
references.push(DynIntrospectable::new::<Bad1>());
references.push(DynIntrospectable::new::<Bad2>());
fn add_references(references: &mut References) {
references.add::<Bad1>();
references.add::<Bad2>();
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/introspection/type_id.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/serialized_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -142,7 +142,7 @@ impl Introspectable for SerializedValue {
LexicalId::VALUE
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}

#[cfg(feature = "fuzzing")]
Expand Down Expand Up @@ -257,5 +257,5 @@ impl Introspectable for SerializedValueSlice {
LexicalId::VALUE
}

fn add_references(_references: &mut Vec<DynIntrospectable>) {}
fn add_references(_references: &mut References) {}
}
Loading

0 comments on commit 34a4c8e

Please sign in to comment.