@@ -433,7 +433,7 @@ const _: () = {
433
433
pub fn default < L : DefaultUnwrappable , R > (
434
434
value : & L ,
435
435
default : R ,
436
- ) -> Result < Either < & L :: Unwrapped , R > , L :: Error > {
436
+ ) -> Result < Either < L :: Unwrapped < ' _ > , R > , L :: Error > {
437
437
match value. try_unwrap ( ) ? {
438
438
Some ( value) => Ok ( Either :: Left ( value) ) ,
439
439
None => Ok ( Either :: Right ( default) ) ,
@@ -447,64 +447,151 @@ pub fn default<L: DefaultUnwrappable, R>(
447
447
) ]
448
448
pub trait DefaultUnwrappable {
449
449
/// The wrapped value
450
- type Unwrapped ;
450
+ type Unwrapped < ' a >
451
+ where
452
+ Self : ' a ;
451
453
452
454
/// An error that prevented [`try_unwrap()`](DefaultUnwrappable::try_unwrap) to succeed,
453
455
/// e.g. a poisoned state or an unacquirable lock.
454
456
type Error : Into < crate :: Error > ;
455
457
456
458
/// Try to unwrap the contained value. Returns `Ok(None)` if the value could not be unwrapped.
457
- fn try_unwrap ( & self ) -> Result < Option < & Self :: Unwrapped > , Self :: Error > ;
459
+ fn try_unwrap ( & self ) -> Result < Option < Self :: Unwrapped < ' _ > > , Self :: Error > ;
458
460
}
459
461
460
- impl_for_ref ! {
461
- impl DefaultUnwrappable for T {
462
- type Unwrapped = T :: Unwrapped ;
463
- type Error = T :: Error ;
462
+ const _: ( ) = {
463
+ impl_for_ref ! {
464
+ impl DefaultUnwrappable for T {
465
+ type Unwrapped <' a> = T :: Unwrapped <' a>
466
+ where
467
+ Self : ' a;
468
+
469
+ type Error = T :: Error ;
470
+
471
+ #[ inline]
472
+ fn try_unwrap( & self ) -> Result <Option <Self :: Unwrapped <' _>>, Self :: Error > {
473
+ <T >:: try_unwrap( self )
474
+ }
475
+ }
476
+ }
477
+
478
+ impl < T > DefaultUnwrappable for Pin < T >
479
+ where
480
+ T : Deref ,
481
+ <T as Deref >:: Target : DefaultUnwrappable ,
482
+ {
483
+ type Unwrapped < ' a >
484
+ = <<T as Deref >:: Target as DefaultUnwrappable >:: Unwrapped < ' a >
485
+ where
486
+ Self : ' a ;
487
+
488
+ type Error = <<T as Deref >:: Target as DefaultUnwrappable >:: Error ;
464
489
465
490
#[ inline]
466
- fn try_unwrap( & self ) -> Result <Option <& Self :: Unwrapped >, Self :: Error > {
467
- < T > :: try_unwrap( self )
491
+ fn try_unwrap ( & self ) -> Result < Option < Self :: Unwrapped < ' _ > > , Self :: Error > {
492
+ self . as_ref ( ) . get_ref ( ) . try_unwrap ( )
468
493
}
469
494
}
470
- }
471
495
472
- impl < T > DefaultUnwrappable for Pin < T >
473
- where
474
- T : Deref ,
475
- <T as Deref >:: Target : DefaultUnwrappable ,
476
- {
477
- type Unwrapped = <<T as Deref >:: Target as DefaultUnwrappable >:: Unwrapped ;
478
- type Error = <<T as Deref >:: Target as DefaultUnwrappable >:: Error ;
496
+ impl < T > DefaultUnwrappable for Option < T > {
497
+ type Unwrapped < ' a >
498
+ = & ' a T
499
+ where
500
+ Self : ' a ;
479
501
480
- #[ inline]
481
- fn try_unwrap ( & self ) -> Result < Option < & Self :: Unwrapped > , Self :: Error > {
482
- self . as_ref ( ) . get_ref ( ) . try_unwrap ( )
502
+ type Error = Infallible ;
503
+
504
+ #[ inline]
505
+ fn try_unwrap ( & self ) -> Result < Option < Self :: Unwrapped < ' _ > > , Self :: Error > {
506
+ Ok ( self . as_ref ( ) )
507
+ }
483
508
}
484
- }
485
509
486
- impl < T > DefaultUnwrappable for Option < T > {
487
- type Unwrapped = T ;
488
- type Error = Infallible ;
510
+ impl < T , E > DefaultUnwrappable for Result < T , E > {
511
+ type Unwrapped < ' a >
512
+ = & ' a T
513
+ where
514
+ Self : ' a ;
489
515
490
- #[ inline]
491
- fn try_unwrap ( & self ) -> Result < Option < & Self :: Unwrapped > , Self :: Error > {
492
- Ok ( self . as_ref ( ) )
516
+ type Error = Infallible ;
517
+
518
+ #[ inline]
519
+ fn try_unwrap ( & self ) -> Result < Option < Self :: Unwrapped < ' _ > > , Self :: Error > {
520
+ match self {
521
+ Ok ( value) => Ok ( Some ( value) ) ,
522
+ Err ( _) => Ok ( None ) ,
523
+ }
524
+ }
493
525
}
494
- }
495
526
496
- impl < T , E > DefaultUnwrappable for Result < T , E > {
497
- type Unwrapped = T ;
498
- type Error = Infallible ;
527
+ impl DefaultUnwrappable for str {
528
+ type Unwrapped < ' a >
529
+ = & ' a str
530
+ where
531
+ Self : ' a ;
499
532
500
- #[ inline]
501
- fn try_unwrap ( & self ) -> Result < Option < & Self :: Unwrapped > , Self :: Error > {
502
- match self {
503
- Ok ( value) => Ok ( Some ( value) ) ,
504
- Err ( _) => Ok ( None ) ,
533
+ type Error = Infallible ;
534
+
535
+ #[ inline]
536
+ fn try_unwrap ( & self ) -> Result < Option < Self :: Unwrapped < ' _ > > , Self :: Error > {
537
+ match self . is_empty ( ) {
538
+ false => Ok ( Some ( self ) ) ,
539
+ true => Ok ( None ) ,
540
+ }
505
541
}
506
542
}
507
- }
543
+
544
+ #[ cfg( feature = "alloc" ) ]
545
+ impl DefaultUnwrappable for alloc:: string:: String {
546
+ type Unwrapped < ' a >
547
+ = & ' a str
548
+ where
549
+ Self : ' a ;
550
+
551
+ type Error = Infallible ;
552
+
553
+ #[ inline]
554
+ fn try_unwrap ( & self ) -> Result < Option < Self :: Unwrapped < ' _ > > , Self :: Error > {
555
+ self . as_str ( ) . try_unwrap ( )
556
+ }
557
+ }
558
+
559
+ macro_rules! impl_for_int {
560
+ ( $( $ty: ty) * ) => { $(
561
+ impl DefaultUnwrappable for $ty {
562
+ type Unwrapped <' a> = Self ;
563
+ type Error = Infallible ;
564
+
565
+ #[ inline]
566
+ fn try_unwrap( & self ) -> Result <Option <Self :: Unwrapped <' _>>, Self :: Error > {
567
+ match * self {
568
+ 0 => Ok ( None ) ,
569
+ value => Ok ( Some ( value) ) ,
570
+ }
571
+ }
572
+ }
573
+ ) * } ;
574
+ }
575
+
576
+ impl_for_int ! (
577
+ u8 u16 u32 u64 u128 usize
578
+ i8 i16 i32 i64 i128 isize
579
+ ) ;
580
+
581
+ impl DefaultUnwrappable for bool {
582
+ type Unwrapped < ' a > = Self ;
583
+
584
+ type Error = Infallible ;
585
+
586
+ #[ inline]
587
+ fn try_unwrap ( & self ) -> Result < Option < Self :: Unwrapped < ' _ > > , Self :: Error > {
588
+ match * self {
589
+ true => Ok ( Some ( true ) ) ,
590
+ false => Ok ( None ) ,
591
+ }
592
+ }
593
+ }
594
+ } ;
508
595
509
596
/// Render either `L` or `R`
510
597
pub enum Either < L , R > {
0 commit comments