Bläddra i källkod

Removed an unnecessary intermediate trait indirection.

Kestrel 7 månader sedan
förälder
incheckning
1c1a600267
2 ändrade filer med 9 tillägg och 58 borttagningar
  1. 4 18
      microrm/src/query.rs
  2. 5 40
      microrm/src/query/components.rs

+ 4 - 18
microrm/src/query.rs

@@ -504,15 +504,12 @@ pub trait IDContainer<T: Entity>: 'static + IntoIterator<Item = T::ID> {
 
 pub trait OutputContainer<T: Entity>: 'static + IntoIterator<Item = Stored<T>> {
     type IDContainer: IDContainer<T>;
+    type ReplacedEntity<N: Entity>: OutputContainer<N>;
     fn assemble_from(conn: &Connection, stmt: StatementContext<'_>) -> DBResult<Self>
     where
         Self: Sized;
 }
 
-pub trait ContainerEntityChange<F: Entity, T: Entity>: OutputContainer<F> {
-    type Container: OutputContainer<T>;
-}
-
 fn assemble_id<T: Entity>(row: StatementRow) -> T::ID {
     <T::ID>::from_raw(row.read::<i64>(0).expect("couldn't read ID"))
 }
@@ -534,6 +531,7 @@ impl<T: Entity> IDContainer<T> for Option<T::ID> {
 
 impl<T: Entity> OutputContainer<T> for Option<Stored<T>> {
     type IDContainer = Option<T::ID>;
+    type ReplacedEntity<N: Entity> = Option<Stored<N>>;
 
     fn assemble_from(conn: &Connection, ctx: StatementContext<'_>) -> DBResult<Self>
     where
@@ -543,10 +541,6 @@ impl<T: Entity> OutputContainer<T> for Option<Stored<T>> {
     }
 }
 
-impl<F: Entity, T: Entity> ContainerEntityChange<F, T> for Option<Stored<F>> {
-    type Container = Option<Stored<T>>;
-}
-
 impl<T: Entity> IDContainer<T> for Vec<T::ID> {
     fn assemble_from(ctx: StatementContext<'_>) -> DBResult<Self>
     where
@@ -560,6 +554,7 @@ impl<T: Entity> IDContainer<T> for Vec<T::ID> {
 
 impl<T: Entity> OutputContainer<T> for Vec<Stored<T>> {
     type IDContainer = Vec<T::ID>;
+    type ReplacedEntity<N: Entity> = Vec<Stored<N>>;
 
     fn assemble_from(conn: &Connection, ctx: StatementContext<'_>) -> DBResult<Self>
     where
@@ -571,10 +566,6 @@ impl<T: Entity> OutputContainer<T> for Vec<Stored<T>> {
     }
 }
 
-impl<F: Entity, T: Entity> ContainerEntityChange<F, T> for Vec<Stored<F>> {
-    type Container = Vec<Stored<T>>;
-}
-
 /// Represents a searchable context of a given entity.
 pub trait Queryable: Clone {
     /// The entity that results from a search in this context.
@@ -803,16 +794,11 @@ pub trait Queryable: Clone {
         part: EP,
     ) -> impl Queryable<
         EntityOutput = <EP::Datum as EntityID>::Entity,
-        OutputContainer = <Self::OutputContainer as ContainerEntityChange<
-            Self::EntityOutput,
-            <EP::Datum as EntityID>::Entity,
-        >>::Container,
+        OutputContainer = <Self::OutputContainer as OutputContainer<Self::EntityOutput>>::ReplacedEntity<<EP::Datum as EntityID>::Entity>,
     >
     where
         Self: Sized,
         EP::Datum: EntityID,
-        Self::OutputContainer:
-            ContainerEntityChange<Self::EntityOutput, <EP::Datum as EntityID>::Entity>,
     {
         components::ForeignComponent::<_, EP, Self>::new(self, part)
     }

+ 5 - 40
microrm/src/query/components.rs

@@ -12,7 +12,7 @@ use crate::{
     },
 };
 
-use super::{ContainerEntityChange, Query};
+use super::{OutputContainer, Query};
 
 /// Allow manipulation of an entire table.
 pub(crate) struct TableComponent<E: Entity> {
@@ -416,7 +416,7 @@ impl<
     }
 }
 
-/// Get an entity via a foreign key
+/// Get an entity via a foreign key.
 pub(crate) struct ForeignComponent<FE: Entity, EP: EntityPart, Parent: Queryable> {
     parent: Parent,
     _ghost: std::marker::PhantomData<(FE, EP)>,
@@ -440,46 +440,11 @@ impl<FE: Entity, EP: EntityPart, Parent: Queryable> Clone for ForeignComponent<F
     }
 }
 
-pub(crate) struct CanonicalForeignComponent<FE: Entity, EP: EntityPart, Parent: Queryable> {
-    _ghost: std::marker::PhantomData<(FE, EP, Parent)>,
-}
-
-impl<FE: Entity, EP: EntityPart, Parent: Queryable> Clone
-    for CanonicalForeignComponent<FE, EP, Parent>
-{
-    fn clone(&self) -> Self {
-        Self {
-            _ghost: Default::default(),
-        }
-    }
-}
-
-impl<FE: Entity, EP: EntityPart, Parent: Queryable> Queryable
-    for CanonicalForeignComponent<FE, EP, Parent>
-{
-    type EntityOutput = FE;
-    type OutputContainer = Option<Stored<FE>>;
-    type StaticVersion = CanonicalForeignComponent<FE, EP, Parent::StaticVersion>;
-
-    fn build(&self) -> Query {
-        unreachable!()
-    }
-    fn bind(&self, _stmt: &mut StatementContext, _index: &mut i32) {
-        unreachable!()
-    }
-    fn conn(&self) -> &Connection {
-        unreachable!()
-    }
-}
-
-impl<FE: Entity, EP: EntityPart, Parent: Queryable> Queryable for ForeignComponent<FE, EP, Parent>
-where
-    Parent::OutputContainer: ContainerEntityChange<Parent::EntityOutput, FE>,
-{
+impl<FE: Entity, EP: EntityPart, Parent: Queryable> Queryable for ForeignComponent<FE, EP, Parent> {
     type EntityOutput = FE;
-    type StaticVersion = CanonicalForeignComponent<FE, EP, Parent::StaticVersion>;
+    type StaticVersion = ForeignComponent<FE, EP, Parent::StaticVersion>;
     type OutputContainer =
-        <Parent::OutputContainer as ContainerEntityChange<Parent::EntityOutput, FE>>::Container;
+        <Parent::OutputContainer as OutputContainer<Parent::EntityOutput>>::ReplacedEntity<FE>;
 
     fn build(&self) -> Query {
         let subquery = self.parent.build().replace(