@@ -325,52 +325,77 @@ impl Borrow<RawDocument> for RawDocumentBuf {
325
325
326
326
/// Types that can be consumed to produce raw bson references valid for a limited lifetime.
327
327
/// Conceptually a union between `T: Into<RawBson>` and `T: Into<RawBsonRef>`; if your type
328
- /// implements `Into<RawBsonRef>` it will automatically implement this, but if it only
329
- /// implements `Into<RawBson>` it will need to manually define the trivial impl.
330
- pub trait BindRawBsonRef {
331
- fn bind < F , R > ( self , f : F ) -> R
332
- where
333
- F : for < ' a > FnOnce ( RawBsonRef < ' a > ) -> R ;
334
- }
328
+ /// implements `Into<RawBsonRef>` it will automatically implement this, but if it
329
+ /// implements `Into<RawBson>` it will need to also define an impl of `BindRawBsonRef`:
330
+ /// ```
331
+ /// # use bson::raw::{BindRawBsonRef, BindValue, RawBson};
332
+ /// # struct MyType;
333
+ /// # impl Into<RawBson> for MyType {
334
+ /// # fn into(self: Self) -> RawBson { todo!() }
335
+ /// # }
336
+ /// impl BindRawBsonRef for MyType {
337
+ /// type Target = BindValue<Self>;
338
+ /// }
339
+ /// ```
340
+ pub trait BindRawBsonRef : Sized {
341
+ type Target : BindHelper < Target = Self > ;
335
342
336
- impl < ' a , T : Into < RawBsonRef < ' a > > > BindRawBsonRef for T {
337
343
fn bind < F , R > ( self , f : F ) -> R
338
344
where
339
- F : for < ' b > FnOnce ( RawBsonRef < ' b > ) -> R ,
345
+ F : for < ' a > FnOnce ( RawBsonRef < ' a > ) -> R ,
340
346
{
341
- f ( self . into ( ) )
347
+ < Self :: Target as BindHelper > :: bind ( self , f )
342
348
}
343
349
}
344
350
345
- impl BindRawBsonRef for RawBson {
346
- fn bind < F , R > ( self , f : F ) -> R
351
+ #[ doc( hidden) ]
352
+ pub trait BindHelper {
353
+ type Target ;
354
+ fn bind < F , R > ( target : Self :: Target , f : F ) -> R
355
+ where
356
+ F : for < ' b > FnOnce ( RawBsonRef < ' b > ) -> R ;
357
+ }
358
+
359
+ /// A struct to use as the `Target` of a [`BindRawBsonRef`] impl for custom types.
360
+ pub struct BindValue < A > ( std:: marker:: PhantomData < A > ) ;
361
+ #[ doc( hidden) ]
362
+ pub struct BindRef < A > ( std:: marker:: PhantomData < A > ) ;
363
+
364
+ impl < A : Into < RawBson > > BindHelper for BindValue < A > {
365
+ type Target = A ;
366
+
367
+ fn bind < F , R > ( target : Self :: Target , f : F ) -> R
347
368
where
348
369
F : for < ' a > FnOnce ( RawBsonRef < ' a > ) -> R ,
349
370
{
350
- f ( self . as_raw_bson_ref ( ) )
371
+ f ( target . into ( ) . as_raw_bson_ref ( ) )
351
372
}
352
373
}
353
374
354
- impl BindRawBsonRef for & RawBson {
355
- fn bind < F , R > ( self , f : F ) -> R
375
+ impl < ' a , A : Into < RawBsonRef < ' a > > + ' a > BindHelper for BindRef < A > {
376
+ type Target = A ;
377
+
378
+ fn bind < F , R > ( target : Self :: Target , f : F ) -> R
356
379
where
357
- F : for < ' a > FnOnce ( RawBsonRef < ' a > ) -> R ,
380
+ F : for < ' b > FnOnce ( RawBsonRef < ' a > ) -> R ,
358
381
{
359
- f ( self . as_raw_bson_ref ( ) )
382
+ f ( target . into ( ) )
360
383
}
361
384
}
362
385
386
+ impl < ' a , T : Into < RawBsonRef < ' a > > + ' a > BindRawBsonRef for T {
387
+ type Target = BindRef < T > ;
388
+ }
389
+
390
+ impl BindRawBsonRef for RawBson {
391
+ type Target = BindValue < Self > ;
392
+ }
393
+
363
394
macro_rules! raw_bson_from_impls {
364
395
( $( $t: ty) ,+$( , ) ?) => {
365
396
$(
366
397
impl BindRawBsonRef for $t {
367
- fn bind<F , R >( self , f: F ) -> R
368
- where
369
- F : for <' a> FnOnce ( RawBsonRef <' a>) -> R ,
370
- {
371
- let tmp: RawBson = self . into( ) ;
372
- f( tmp. as_raw_bson_ref( ) )
373
- }
398
+ type Target = BindValue <Self >;
374
399
}
375
400
) +
376
401
} ;
0 commit comments