소스 검색

Add helper trait for working with Serialized<> types.

Kestrel 7 달 전
부모
커밋
3fea4ab169
2개의 변경된 파일25개의 추가작업 그리고 2개의 파일을 삭제
  1. 1 1
      microrm/src/lib.rs
  2. 24 1
      microrm/src/schema.rs

+ 1 - 1
microrm/src/lib.rs

@@ -235,7 +235,7 @@ pub mod cli;
 /// Module prelude with commonly-used traits that shouldn't have overlap with other crates.
 pub mod prelude {
     pub use crate::query::{Insertable, Queryable, RelationInterface};
-    pub use crate::schema::{relation::Relation, Database};
+    pub use crate::schema::{relation::Relation, Database, Serializable};
     pub use microrm_macros::{Database, Entity};
 }
 

+ 24 - 1
microrm/src/schema.rs

@@ -214,17 +214,40 @@ impl<T: 'static + serde::Serialize + serde::de::DeserializeOwned + std::fmt::Deb
 {
 }
 
+/// Helper trait to make working with [`Serialized`] fields a little bit nicer.
+pub trait Serializable: serde::Serialize + serde::de::DeserializeOwned + std::fmt::Debug + Clone {
+    /// Wrap an eligible object into a [`Serialized`] version of itself.
+    fn into_serialized(self) -> Serialized<Self> where Self: Sized;
+}
+
+impl<T: 'static + serde::Serialize + serde::de::DeserializeOwned + std::fmt::Debug + Clone> Serializable for T {
+    fn into_serialized(self) -> Serialized<Self> where Self: Sized {
+        Serialized {
+            wrapped: self
+        }
+    }
+}
+
+
 // ----------------------------------------------------------------------
 // Database specification types
 // ----------------------------------------------------------------------
 
 /// Table with EntityID-based lookup.
-#[derive(Clone)]
 pub struct IDMap<T: Entity> {
     pub(crate) conn: Connection,
     _ghost: std::marker::PhantomData<T>,
 }
 
+impl<T: Entity> Clone for IDMap<T> {
+    fn clone(&self) -> Self {
+        Self {
+            conn: self.conn.clone(),
+            _ghost: Default::default(),
+        }
+    }
+}
+
 impl<T: Entity> IDMap<T> {
     /// Construct a non-empty instance of an `IDMap`.
     pub fn build(db: Connection) -> Self {