@@ -4,6 +4,7 @@ use crate::{HasParamSpec, Property, PropertyGet, PropertySet, PropertySetNested}
4
4
5
5
use once_cell:: sync:: OnceCell as SyncOnceCell ;
6
6
use once_cell:: unsync:: OnceCell ;
7
+ use std:: cell:: Cell ;
7
8
use std:: cell:: RefCell ;
8
9
use std:: sync:: Mutex ;
9
10
use std:: sync:: RwLock ;
@@ -100,3 +101,47 @@ define_construct!(@with_uninit_type ConstructSyncOnceCell, SyncOnceCell, T,
100
101
oc
101
102
}
102
103
) ;
104
+
105
+ // Manual implementation because Cell often requires `Copy`, so `Debug` can't be derived,
106
+ // `PropertyGet` and `PropertySet` can't be generated as with the other types...
107
+ pub struct ConstructCell < T > ( Cell < Option < T > > ) ;
108
+ impl < T > Default for ConstructCell < T > {
109
+ fn default ( ) -> Self {
110
+ Self :: new_empty ( )
111
+ }
112
+ }
113
+ impl < T : Property + HasParamSpec > Property for ConstructCell < T > {
114
+ type Value = T ;
115
+ }
116
+
117
+ impl < T > std:: ops:: Deref for ConstructCell < T > {
118
+ type Target = Cell < Option < T > > ;
119
+ fn deref ( & self ) -> & Self :: Target {
120
+ & self . 0
121
+ }
122
+ }
123
+ impl < T > ConstructCell < T > {
124
+ pub fn new ( value : T ) -> Self {
125
+ Self ( Cell :: new ( Some ( value) ) )
126
+ }
127
+ pub fn new_empty ( ) -> Self {
128
+ Self ( Cell :: default ( ) )
129
+ }
130
+ }
131
+ impl < T : Copy + std:: fmt:: Debug > std:: fmt:: Debug for ConstructCell < T > {
132
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
133
+ f. debug_struct ( "ConstructCell" ) . field ( "0" , & self . 0 ) . finish ( )
134
+ }
135
+ }
136
+ impl < T : Copy > PropertySet for ConstructCell < T > {
137
+ type SetValue = T ;
138
+ fn set ( & self , v : T ) {
139
+ PropertySet :: set ( & self . 0 , Some ( v) )
140
+ }
141
+ }
142
+ impl < T : Copy > PropertyGet for ConstructCell < T > {
143
+ type Value = T ;
144
+ fn get < R , F : Fn ( & Self :: Value ) -> R > ( & self , f : F ) -> R {
145
+ PropertyGet :: get ( & self . 0 , |v| f ( v. as_ref ( ) . unwrap ( ) ) )
146
+ }
147
+ }
0 commit comments