Diffstat (limited to 'tests/builder_struct.rs')
| -rw-r--r-- | tests/builder_struct.rs | 148 |
1 files changed, 20 insertions, 128 deletions
diff --git a/tests/builder_struct.rs b/tests/builder_struct.rs index 290676b..f77bd52 100644 --- a/tests/builder_struct.rs +++ b/tests/builder_struct.rs @@ -1,3 +1,4 @@ +use macro_rules_attribute::derive; use treaty::{ any::{OwnedStatic, TempBorrowedStatic}, builders::{self, core::r#struct::StructBuilder}, @@ -9,10 +10,7 @@ use treaty::{ transform, walkers::{ self, - core::{ - noop::NoopWalker, - r#struct::{StaticType, StructWalker}, - }, + core::{noop::NoopWalker, r#struct::StructWalker}, }, Build, Builder, DefaultMode, Flow, Walk, Walker, }; @@ -21,136 +19,19 @@ use crate::common::{protocol::sequence::MockSequenceScope, walker::MockWalker}; mod common; -#[derive(Debug, PartialEq)] +#[derive(Build!, Walk!, Debug, PartialEq)] struct X { a: bool, b: bool, } -struct Info; - -impl<'ctx, M> walkers::core::r#struct::StructTypeInfo<'ctx, M> for Info { - const NAME: &'static str = "X"; - - const FIELDS: &'static [&'static str] = &["a", "b"]; - - type FieldError = (); - - type S = StaticType; - - type T = X; - - #[inline(always)] - fn walk_field<'a, E: Effect>( - index: usize, - value: &'ctx Self::T, - visitor: DynVisitor<'a, 'ctx>, - ) -> ErasedEffective<'a, Result<Flow, Self::FieldError>, E> { - todo!() - // E::wrap(async move { - // match index { - // 0 => { - // let walker = <&bool as Walk<M, E>>::into_walker(&value.a); - // - // assert_eq!(Walker::<E>::walk(walker, visitor).await, Ok(())); - // - // Ok(Flow::Continue) - // } - // 1 => { - // let walker = <&bool as Walk<M, E>>::into_walker(&value.b); - // - // assert_eq!(Walker::<E>::walk(walker, visitor).await, Ok(())); - // - // Ok(Flow::Continue) - // } - // _ => Ok(Flow::Done), - // } - // }) - } -} - -struct Fields<'ctx, M, E: Effect> { - a: <bool as Build<'ctx, M, E>>::Builder, - b: <bool as Build<'ctx, M, E>>::Builder, -} - -#[derive(Copy, Clone, Debug)] -enum FieldMarker { - A, - B, -} - -impl<'ctx, M, E: Effect> builders::core::r#struct::StructTypeInfo<'ctx, M, E> for Info { - type Builders = Fields<'ctx, M, E>; - - type FieldMarker = FieldMarker; - - type T = X; - - #[inline(always)] - fn marker_from_index(index: usize) -> Option<Self::FieldMarker> { - match index { - 0 => Some(FieldMarker::A), - 1 => Some(FieldMarker::B), - _ => None, - } - } - - #[inline(always)] - fn marker_from_name(name: &str) -> Option<Self::FieldMarker> { - match name { - "a" => Some(FieldMarker::A), - "b" => Some(FieldMarker::B), - _ => None, - } - } - - type Error = (); - - #[inline(always)] - fn from_builders<'a>( - builders: Self::Builders, - ) -> ErasedEffective<'a, Result<Self::T, Self::Error>, E> { - E::from_future(async { - Ok(X { - a: builders.a.build().into_future().await.unwrap(), - b: builders.b.build().into_future().await.unwrap(), - }) - }) - } - - #[inline(always)] - fn as_visitor<'a>( - marker: Self::FieldMarker, - builders: &'a mut Self::Builders, - ) -> DynVisitor<'a, 'ctx> { - match marker { - FieldMarker::A => builders.a.as_visitor(), - FieldMarker::B => builders.b.as_visitor(), - } - } - - type Seed = (); - - #[inline(always)] - fn new_builders<'a>(_seed: Self::Seed) -> ErasedEffective<'a, Self::Builders, E> { - E::from_future(async { - Fields { - a: Builder::<E>::from_seed(()).into_future().await, - b: Builder::<E>::from_seed(()).into_future().await, - } - }) - } -} - #[test] -#[ignore] fn demo() { let value = X { a: true, b: false }; - let (other, _) = transform::<StructBuilder<Info, DefaultMode, _>, _, Blocking>( - (), - StructWalker::<Info, _, DefaultMode, _>::new(&value), + let (other, _) = transform::<<X as Build<DefaultMode, _>>::Builder, _, Blocking>( + ((), ()), + <&X as Walk<DefaultMode, _>>::Walker::new(&value), ) .value(); @@ -190,7 +71,7 @@ fn from_basic_tuple_like() { Flow::Done }); - let mut builder = StructBuilder::<Info, DefaultMode, Blocking>::from_seed(()).value(); + let mut builder = <X as Build<DefaultMode, Blocking>>::Builder::from_seed(((), ())).value(); let visitor = builder.as_visitor(); // Visit the sequence of field values. @@ -270,7 +151,7 @@ fn from_basic_map_like() { // There are no more fields. scope.expect_next().once().returning(|_visitor| Flow::Done); - let mut builder = StructBuilder::<Info, DefaultMode, Blocking>::from_seed(()).value(); + let mut builder = <X as Build<DefaultMode, Blocking>>::Builder::from_seed(((), ())).value(); let mut visitor = builder.as_visitor(); // We need to provide the map tag to the struct before getting into the sequence. @@ -333,6 +214,17 @@ pub mod demo { #[test] fn demo() { - assert_eq!(ident(X { a: true, b: false, c: true }), Y { a: true, b: false, c: true }); + assert_eq!( + ident(X { + a: true, + b: false, + c: true + }), + Y { + a: true, + b: false, + c: true + } + ); } } |