File tree 1 file changed +22
-0
lines changed
1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 65
65
} )
66
66
}
67
67
68
+ pub fn unsafe_new ( value : T ) -> Self {
69
+ Self {
70
+ value : RULE :: validate ( value)
71
+ . ok ( )
72
+ . expect ( "initialization by `unsafe_new` failed" ) ,
73
+ _rule : Default :: default ( ) ,
74
+ }
75
+ }
76
+
68
77
pub fn value ( & self ) -> & RULE :: Item {
69
78
& self . value
70
79
}
@@ -92,6 +101,19 @@ mod test {
92
101
use serde:: { Deserialize , Serialize } ;
93
102
use serde_json:: json;
94
103
104
+ #[ test]
105
+ fn test_unsafe_new_success ( ) {
106
+ let non_empty_string = Refined :: < NonEmptyStringRule > :: unsafe_new ( "Hello" . to_string ( ) ) ;
107
+ assert_eq ! ( non_empty_string. value, "Hello" ) ;
108
+ }
109
+
110
+ #[ test]
111
+ #[ should_panic( expected = "initialization by `unsafe_new` failed" ) ]
112
+ fn test_unsafe_new_panic ( ) {
113
+ let non_empty_string = Refined :: < NonEmptyStringRule > :: unsafe_new ( "" . to_string ( ) ) ;
114
+ assert_eq ! ( non_empty_string. value, "" ) ; // unreachable
115
+ }
116
+
95
117
#[ test]
96
118
fn test_refined_non_empty_string_ok ( ) -> Result < ( ) , Error < String > > {
97
119
let non_empty_string = Refined :: < NonEmptyStringRule > :: new ( "Hello" . to_string ( ) ) ?;
You can’t perform that action at this time.
0 commit comments