refine macros

This commit is contained in:
Aiden McClelland
2021-03-25 18:09:07 -06:00
parent 2c3fd8159e
commit 5b77719631
5 changed files with 85 additions and 39 deletions

View File

@@ -19,6 +19,7 @@ serde_json = "1.0.61"
serde_cbor = { path = "../cbor" }
thiserror = "1.0.23"
tokio = { version = "1.0.1", features = ["sync", "fs", "rt", "io-util", "macros"] }
patch-db-macro = { path = "../patch-db-macro" }
[dev-dependencies]
serde = { version = "1.0.118", features = ["rc", "derive"] }

View File

@@ -1002,13 +1002,14 @@ impl<T: Serialize + for<'de> Deserialize<'de>> DerefMut for ModelDataMut<T> {
}
}
#[derive(Debug)]
pub struct Model<T: Serialize + for<'de> Deserialize<'de>> {
ptr: JsonPointer,
phantom: PhantomData<T>,
}
impl<T> Model<T>
where
T: Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
T: Serialize + for<'de> Deserialize<'de>,
{
pub fn new(ptr: JsonPointer) -> Self {
Self {
@@ -1045,3 +1046,18 @@ where
}
}
}
impl<T> std::clone::Clone for Model<T>
where
T: Serialize + for<'de> Deserialize<'de>,
{
fn clone(&self) -> Self {
Model {
ptr: self.ptr.clone(),
phantom: PhantomData,
}
}
}
pub trait HasModel {
type Model;
}

View File

@@ -1,4 +1,6 @@
use super::*;
use crate as patch_db;
use patch_db_macro::HasModel;
#[tokio::test]
async fn basic() {
@@ -21,24 +23,15 @@ async fn basic() {
assert_eq!(get_res, "hello");
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[derive(Debug, serde::Deserialize, serde::Serialize, HasModel)]
pub struct Sample {
a: String,
#[model(name = ChildModel)]
b: Child,
}
pub struct SampleModel(Model<Sample>);
impl core::ops::Deref for SampleModel {
type Target = Model<Sample>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Child {
a: String,
b: usize,
}
pub struct ChildModel(Model<Child>);