1use std::{
2 cell::{
3 Cell,
4 RefCell,
5 },
6 marker::PhantomData,
7 rc::Rc,
8 sync::{
9 Arc,
10 Mutex,
11 },
12};
13use crate::{
14 FormWith,
15 FormElements,
16 FormState,
17};
18
19struct RcFormState<C, T: FormWith<C>>(Box<dyn FormState<T>>, PhantomData<C>);
20
21impl<C, T: FormWith<C>> FormState<Rc<T>> for RcFormState<C, T> {
22 fn parse(&self) -> Result<Rc<T>, ()> {
23 return Ok(Rc::new(self.0.parse()?));
24 }
25}
26
27impl<C: 'static, T: FormWith<C> + 'static> FormWith<C> for Rc<T> {
28 fn new_form_with_(
29 context: &C,
30 field: &str,
31 from: Option<&Self>,
32 depth: usize,
33 ) -> (FormElements, Box<dyn FormState<Self>>) {
34 let (elements, state) = T::new_form_with_(context, field, from.map(|x| x.as_ref()), depth);
35 return (elements, Box::new(RcFormState(state, Default::default())));
36 }
37}
38
39struct ArcFormState<C, T: FormWith<C>>(Box<dyn FormState<T>>, PhantomData<C>);
40
41impl<C, T: FormWith<C>> FormState<Arc<T>> for ArcFormState<C, T> {
42 fn parse(&self) -> Result<Arc<T>, ()> {
43 return Ok(Arc::new(self.0.parse()?));
44 }
45}
46
47impl<C: 'static, T: FormWith<C> + 'static> FormWith<C> for Arc<T> {
48 fn new_form_with_(
49 context: &C,
50 field: &str,
51 from: Option<&Self>,
52 depth: usize,
53 ) -> (FormElements, Box<dyn FormState<Self>>) {
54 let (elements, state) = T::new_form_with_(context, field, from.map(|x| x.as_ref()), depth);
55 return (elements, Box::new(ArcFormState(state, Default::default())));
56 }
57}
58
59struct CellFormState<C, T: FormWith<C>>(Box<dyn FormState<T>>, PhantomData<C>);
60
61impl<C, T: FormWith<C> + Copy + Clone> FormState<Cell<T>> for CellFormState<C, T> {
62 fn parse(&self) -> Result<Cell<T>, ()> {
63 return Ok(Cell::new(self.0.parse()?));
64 }
65}
66
67impl<C: 'static, T: FormWith<C> + Copy + Clone + 'static> FormWith<C> for Cell<T> {
68 fn new_form_with_(
69 context: &C,
70 field: &str,
71 from: Option<&Self>,
72 depth: usize,
73 ) -> (FormElements, Box<dyn FormState<Self>>) {
74 let (elements, state) = T::new_form_with_(context, field, from.map(|x| x.get()).as_ref(), depth);
75 return (elements, Box::new(CellFormState(state, Default::default())));
76 }
77}
78
79struct RefCellFormState<C, T: FormWith<C>>(Box<dyn FormState<T>>, PhantomData<C>);
80
81impl<C, T: FormWith<C>> FormState<RefCell<T>> for RefCellFormState<C, T> {
82 fn parse(&self) -> Result<RefCell<T>, ()> {
83 return Ok(RefCell::new(self.0.parse()?));
84 }
85}
86
87impl<C: 'static, T: FormWith<C> + 'static> FormWith<C> for RefCell<T> {
88 fn new_form_with_(
89 context: &C,
90 field: &str,
91 from: Option<&Self>,
92 depth: usize,
93 ) -> (FormElements, Box<dyn FormState<Self>>) {
94 let from = from.map(|x| x.borrow());
95 let (elements, state) = T::new_form_with_(context, field, match &from {
96 Some(x) => Some(&*x),
97 None => None,
98 }, depth);
99 return (elements, Box::new(RefCellFormState(state, Default::default())));
100 }
101}
102
103struct MutexFormState<C, T: FormWith<C>>(Box<dyn FormState<T>>, PhantomData<C>);
104
105impl<C, T: FormWith<C>> FormState<Mutex<T>> for MutexFormState<C, T> {
106 fn parse(&self) -> Result<Mutex<T>, ()> {
107 return Ok(Mutex::new(self.0.parse()?));
108 }
109}
110
111impl<C: 'static, T: FormWith<C> + 'static> FormWith<C> for Mutex<T> {
112 fn new_form_with_(
113 context: &C,
114 field: &str,
115 from: Option<&Self>,
116 depth: usize,
117 ) -> (FormElements, Box<dyn FormState<Self>>) {
118 let from = from.map(|x| x.lock().unwrap());
119 let (elements, state) = T::new_form_with_(context, field, match &from {
120 Some(x) => Some(&*x),
121 None => None,
122 }, depth);
123 return (elements, Box::new(MutexFormState(state, Default::default())));
124 }
125}