1
1
use nutype:: nutype;
2
2
use std:: borrow:: Cow ;
3
+ use std:: collections:: HashMap ;
3
4
use test_suite:: test_helpers:: traits:: * ;
4
5
5
6
// Inner custom type, which is unknown to nutype
@@ -488,7 +489,7 @@ mod with_generics {
488
489
fn test_generic_with_lifetime_cow ( ) {
489
490
#[ nutype(
490
491
validate( predicate = |s| s. len( ) >= 3 ) ,
491
- derive( Debug , Display , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Into , Deref , Borrow , TryFrom )
492
+ derive( Debug , Display , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Into , Deref , Borrow , TryFrom , AsRef )
492
493
) ]
493
494
struct Clarabelle < ' a > ( Cow < ' a , str > ) ;
494
495
@@ -500,4 +501,31 @@ mod with_generics {
500
501
assert_eq ! ( muu. to_string( ) , "Muu" ) ;
501
502
}
502
503
}
504
+
505
+ #[ test]
506
+ fn test_derive_as_ref_with_generic ( ) {
507
+ #[ nutype( derive( AsRef ) ) ]
508
+ struct SquareMap < K , V > ( HashMap < K , V > ) ;
509
+
510
+ let mut inner_map = HashMap :: new ( ) ;
511
+ inner_map. insert ( 4 , 16 ) ;
512
+ inner_map. insert ( 5 , 25 ) ;
513
+ let squares = SquareMap :: new ( inner_map. clone ( ) ) ;
514
+ assert_eq ! ( squares. as_ref( ) , & inner_map) ;
515
+ }
516
+
517
+ #[ test]
518
+ fn test_derive_as_ref_with_generic_and_validation ( ) {
519
+ #[ nutype(
520
+ validate( predicate = |map| map. len( ) > 1 ) ,
521
+ derive( AsRef )
522
+ ) ]
523
+ struct NonEmptyMap < K , V > ( HashMap < K , V > ) ;
524
+
525
+ let mut inner_map = HashMap :: new ( ) ;
526
+ inner_map. insert ( 4 , 16 ) ;
527
+ inner_map. insert ( 5 , 25 ) ;
528
+ let squares = NonEmptyMap :: new ( inner_map. clone ( ) ) . unwrap ( ) ;
529
+ assert_eq ! ( squares. as_ref( ) , & inner_map) ;
530
+ }
503
531
}
0 commit comments