5656//! .await?;
5757//! assert_eq!(by_email, stored.email.terms);
5858//! let adults = EncryptedUser::AGE
59- //! .between(& 18u32, & 36)
59+ //! .between(18u32, 36)
6060//! .scope(Scope::new(42u64))
6161//! .encrypt(&keyset)
6262//! .await?;
7575//! # What a marker does not check
7676//!
7777//! An equality marker takes a value of any type that the equality index
78- //! accepts: `EMAIL.eq(& 42u64)` compiles on a text field and matches
78+ //! accepts: `EMAIL.eq(42u64)` compiles on a text field and matches
7979//! nothing, because the derive sees the record's field types and not the
8080//! plaintext struct's. An order marker fixes the type, since `OreTerm<S>`
8181//! names it. A field whose type is several indexes, such as
@@ -203,22 +203,95 @@ fn wipe_pair<S: Zeroize>((mut lo, mut hi): (S, S)) {
203203 hi. zeroize ( ) ;
204204}
205205
206+ /// What a marker's operator methods take: an owned scalar, moved into the
207+ /// query, or a borrowed text or byte string, copied into it. A number is
208+ /// given by value, `AGE.between(18, 65)`, and a string literal as it is,
209+ /// `EMAIL.eq("ada@example.com")`. The query holds the owned value and
210+ /// wipes it when it is derived, refused or dropped. The scalars are the
211+ /// ones the indexes accept: the booleans, the integers, the floats, text,
212+ /// bytes, the [`value`](crate::value) types, and an `Option` of each.
213+ pub trait QueryInput {
214+ /// The owned value the query holds.
215+ type Value : Zeroize + ' static ;
216+ /// The value, owned.
217+ fn into_value ( self ) -> Self :: Value ;
218+ }
219+ macro_rules! owned_query_inputs {
220+ ( $( $ty: ty) ,+ $( , ) ?) => { $(
221+ impl QueryInput for $ty {
222+ type Value = $ty;
223+ fn into_value( self ) -> $ty {
224+ self
225+ }
226+ }
227+ ) +} ;
228+ }
229+ owned_query_inputs ! (
230+ bool ,
231+ u8 ,
232+ u16 ,
233+ u32 ,
234+ u64 ,
235+ u128 ,
236+ i8 ,
237+ i16 ,
238+ i32 ,
239+ i64 ,
240+ i128 ,
241+ f32 ,
242+ f64 ,
243+ String ,
244+ Vec <u8 >,
245+ crate :: value:: Date ,
246+ crate :: value:: Timestamp ,
247+ crate :: value:: Decimal ,
248+ ) ;
249+ impl < V : QueryInput > QueryInput for Option < V > {
250+ type Value = Option < V :: Value > ;
251+ fn into_value ( self ) -> Self :: Value {
252+ self . map ( V :: into_value)
253+ }
254+ }
255+ impl QueryInput for & str {
256+ type Value = String ;
257+ fn into_value ( self ) -> String {
258+ self . to_owned ( )
259+ }
260+ }
261+ impl QueryInput for & String {
262+ type Value = String ;
263+ fn into_value ( self ) -> String {
264+ self . clone ( )
265+ }
266+ }
267+ impl QueryInput for & [ u8 ] {
268+ type Value = Vec < u8 > ;
269+ fn into_value ( self ) -> Vec < u8 > {
270+ self . to_vec ( )
271+ }
272+ }
273+ impl QueryInput for & Vec < u8 > {
274+ type Value = Vec < u8 > ;
275+ fn into_value ( self ) -> Vec < u8 > {
276+ self . clone ( )
277+ }
278+ }
279+
206280/// One method per operator. Each exists only where the field's target type
207281/// answers the operator ([`Answers`]), so a missing one is a compile error
208- /// that names the field's indexes. The value is copied into the query
209- /// (`ToOwned` ), and the copy is wiped when the query is derived, refused or
282+ /// that names the field's indexes. The value is moved or copied into the
283+ /// query ([`QueryInput`] ), and wiped when the query is derived, refused or
210284/// dropped.
211285macro_rules! operator_methods {
212286 ( $( $( #[ $doc: meta] ) * $method: ident => $op: ident; ) +) => { $(
213287 impl <T , C : ScopeInput > Marker <T , C > {
214288 $( #[ $doc] ) *
215- pub fn $method<Q >( self , value: & Q ) -> MarkedQuery <T , $op, Q :: Owned , C :: Start >
289+ pub fn $method<Q >( self , value: Q ) -> MarkedQuery <T , $op, Q :: Value , C :: Start >
216290 where
217- Q : ?Sized + ToOwned ,
218- Q :: Owned : Zeroize + ' static ,
219- T : Answers <$op, Q :: Owned > + LabelledTarget ,
291+ Q : QueryInput ,
292+ T : Answers <$op, Q :: Value > + LabelledTarget ,
220293 {
221- self . query( value. to_owned ( ) , wipe_one)
294+ self . query( value. into_value ( ) , wipe_one)
222295 }
223296 }
224297 ) +} ;
@@ -244,13 +317,12 @@ impl<T, C: ScopeInput> Marker<T, C> {
244317 /// Rows whose value is between `lo` and `hi`, inclusive
245318 /// ([`op::Between`](Between)): one term carrying both bounds, derived
246319 /// under one context.
247- pub fn between < Q > ( self , lo : & Q , hi : & Q ) -> BetweenQuery < T , Q :: Owned , C :: Start >
320+ pub fn between < Q > ( self , lo : Q , hi : Q ) -> BetweenQuery < T , Q :: Value , C :: Start >
248321 where
249- Q : ?Sized + ToOwned ,
250- Q :: Owned : Zeroize + ' static ,
251- T : Answers < Between , ( Q :: Owned , Q :: Owned ) > + LabelledTarget ,
322+ Q : QueryInput ,
323+ T : Answers < Between , ( Q :: Value , Q :: Value ) > + LabelledTarget ,
252324 {
253- self . query ( ( lo. to_owned ( ) , hi. to_owned ( ) ) , wipe_pair)
325+ self . query ( ( lo. into_value ( ) , hi. into_value ( ) ) , wipe_pair)
254326 }
255327}
256328
@@ -444,4 +516,48 @@ mod tests {
444516 assert_eq ! ( wipes. load( Ordering :: SeqCst ) , 0 , "nothing left to wipe" ) ;
445517 drop ( taken) ;
446518 }
519+
520+ /// An owned scalar is the query's as it is, for each scalar the
521+ /// indexes accept; a borrowed text or byte string is copied into an
522+ /// owned one.
523+ #[ test]
524+ fn a_query_input_is_owned_or_copied_in ( ) {
525+ use crate :: value:: { Date , Decimal , Timestamp } ;
526+ macro_rules! owned_as_it_is {
527+ ( $( $value: expr) ,+ $( , ) ?) => { $(
528+ assert_eq!( $value. into_value( ) , $value) ;
529+ assert_eq!( Some ( $value) . into_value( ) , Some ( $value) ) ;
530+ ) +} ;
531+ }
532+ owned_as_it_is ! (
533+ true ,
534+ 7u8 ,
535+ 7u16 ,
536+ 7u32 ,
537+ 7u64 ,
538+ 7u128 ,
539+ -7i8 ,
540+ -7i16 ,
541+ -7i32 ,
542+ -7i64 ,
543+ -7i128 ,
544+ 0.5f32 ,
545+ 0.5f64 ,
546+ String :: from( "ada" ) ,
547+ b"ada" . to_vec( ) ,
548+ ) ;
549+ let date = Date :: from_days_since_epoch ( 7 ) . unwrap ( ) ;
550+ assert_eq ! ( date. into_value( ) . days_since_epoch( ) , 7 ) ;
551+ let instant = Timestamp :: new ( 7 , 9 ) . unwrap ( ) ;
552+ let instant = instant. into_value ( ) ;
553+ assert_eq ! ( ( instant. seconds( ) , instant. nanoseconds( ) ) , ( 7 , 9 ) ) ;
554+ let decimal = Decimal :: new ( 75 , 1 ) . unwrap ( ) ;
555+ let decimal = decimal. into_value ( ) ;
556+ assert_eq ! ( ( decimal. mantissa( ) , decimal. scale( ) ) , ( 75 , 1 ) ) ;
557+
558+ assert_eq ! ( "ada" . into_value( ) , String :: from( "ada" ) ) ;
559+ assert_eq ! ( ( & String :: from( "ada" ) ) . into_value( ) , String :: from( "ada" ) ) ;
560+ assert_eq ! ( b"ada" [ ..] . into_value( ) , b"ada" . to_vec( ) ) ;
561+ assert_eq ! ( ( & b"ada" . to_vec( ) ) . into_value( ) , b"ada" . to_vec( ) ) ;
562+ }
447563}
0 commit comments