Diffstat (limited to 'tests/demo.rs')
| -rw-r--r-- | tests/demo.rs | 84 |
1 files changed, 52 insertions, 32 deletions
diff --git a/tests/demo.rs b/tests/demo.rs index 9baf602..0f0e190 100644 --- a/tests/demo.rs +++ b/tests/demo.rs @@ -1,10 +1,12 @@ use std::{collections::VecDeque, ops::ControlFlow}; use treaty::{ any::{any_trait, static_wrapper::OwnedStatic}, - build_with, into_walker, + build, build_with, + builders::core::array, + into_walker, protocol::{ visitor::{Sequence, SequenceScope, Value}, - SyncEffect, Visitor, + ControlFlowFor, Ready, SyncEffect, Visitor, }, Builder, Walk, Walker, }; @@ -17,9 +19,20 @@ fn demo() { Data::Bool(false), ]); - let s = build_with::<JsonLike, _>(into_walker(a)).unwrap(); + // dbg!(array_to_array([true, false, true])); - assert_eq!(s, "[true,[false,true,],false,]"); + let s = build_with::<array::Builder<'_, JsonLike, 3>, _>(into_walker(&a)).unwrap(); + dbg!(s); + + // let s = build_with::<JsonLike, _>(into_walker(a)).unwrap(); + + todo!(); + // assert_eq!(s, "[true,[false,true,],false,]"); +} + +#[no_mangle] +pub fn array_to_array(x: [bool; 1]) -> [bool; 1] { + unsafe { build(into_walker(x)).unwrap_unchecked() } } #[derive(Debug)] @@ -29,64 +42,68 @@ enum Data { } const _: () = { - struct Impl(Data); + struct Impl<'ctx>(&'ctx Data); - impl From<Data> for Impl { - fn from(value: Data) -> Self { + impl<'ctx> From<&'ctx Data> for Impl<'ctx> { + fn from(value: &'ctx Data) -> Self { Self(value) } } - impl<'ctx> Walk<'ctx> for Data { - type Walker = Impl; + impl<'ctx> Walk<'ctx> for &'ctx Data { + type Walker = Impl<'ctx>; } - impl<'ctx> Walker<'ctx> for Impl { + impl<'ctx> Walker<'ctx> for Impl<'ctx> { type Effect = SyncEffect; type Error = (); type Output = (); - fn walk( + fn walk<'a>( self, - visitor: &mut Visitor<'ctx>, - ) -> core::ops::ControlFlow<Self::Error, Self::Output> { + visitor: &'a mut Visitor<'a, 'ctx>, + ) -> ControlFlowFor<'a, SyncEffect, Self::Error, Self::Output> { match self.0 { - Data::Bool(value) => walk_bool(value, visitor), + Data::Bool(value) => walk_bool(*value, visitor), Data::Sequence(value) => walk_vec(value, visitor), } - core::ops::ControlFlow::Continue(()) + Ready::new(core::ops::ControlFlow::Continue(())) } } }; -fn walk_bool(value: bool, visitor: &mut Visitor<'_>) { +fn walk_bool(value: bool, visitor: &mut Visitor<'_, '_>) { visitor .upcast_mut::<dyn Value<OwnedStatic<bool>>>() .unwrap() .visit(OwnedStatic(value)); } -fn walk_vec(value: Vec<Data>, visitor: &mut Visitor<'_>) { - struct Scope(VecDeque<Data>); +fn walk_vec<'a, 'ctx>(value: &'ctx [Data], visitor: &'a mut Visitor<'a, 'ctx>) { + struct Scope<'ctx>(VecDeque<&'ctx Data>); - impl<'ctx> SequenceScope<'ctx> for Scope { - fn next( - &mut self, - visitor: &mut Visitor<'ctx>, - ) -> core::ops::ControlFlow<(), treaty::protocol::visitor::Status> { + impl<'ctx> SequenceScope<'ctx> for Scope<'ctx> { + fn next<'a>( + &'a mut self, + visitor: &'a mut Visitor<'a, 'ctx>, + ) -> ControlFlowFor<'a, SyncEffect, treaty::protocol::visitor::Status> { if let Some(value) = self.0.pop_front() { into_walker(value).walk(visitor); - ControlFlow::Continue(treaty::protocol::visitor::Status::Continue) + Ready::new(ControlFlow::Continue( + treaty::protocol::visitor::Status::Continue, + )) } else { - ControlFlow::Continue(treaty::protocol::visitor::Status::Done) + Ready::new(ControlFlow::Continue( + treaty::protocol::visitor::Status::Done, + )) } } } - let mut scope = Scope(value.into()); + let mut scope = Scope(value.into_iter().collect()); visitor .upcast_mut::<dyn Sequence<'_>>() @@ -102,7 +119,7 @@ impl<'ctx> Builder<'ctx> for JsonLike { type Value = String; - fn as_visitor(&mut self) -> &mut Visitor<'ctx> { + fn as_visitor(&mut self) -> &mut Visitor<'_, 'ctx> { self } @@ -119,21 +136,24 @@ any_trait! { } impl Value<'_, OwnedStatic<bool>> for JsonLike { - fn visit(&mut self, value: OwnedStatic<bool>) -> core::ops::ControlFlow<()> { + fn visit(&mut self, value: OwnedStatic<bool>) -> ControlFlowFor<'_> { self.0.push_str(&format!("{}", value.0)); - ControlFlow::Continue(()) + Ready::new(ControlFlow::Continue(())) } } impl<'ctx> Sequence<'ctx> for JsonLike { - fn visit(&mut self, scope: &mut dyn SequenceScope<'ctx>) -> core::ops::ControlFlow<()> { + fn visit<'a>( + &'a mut self, + scope: &'a mut dyn SequenceScope<'ctx>, + ) -> ControlFlowFor<'a, SyncEffect> { self.0.push_str("["); while let ControlFlow::Continue(treaty::protocol::visitor::Status::Continue) = - scope.next(self) + scope.next(self).into_inner() { self.0.push_str(","); } self.0.push_str("]"); - ControlFlow::Continue(()) + Ready::new(ControlFlow::Continue(())) } } |