@@ -2455,6 +2455,56 @@ fn test_boxed_raw_value() {
24552455 assert_eq ! ( r#"["a",42,{"foo": "bar"},null]"# , array_to_string) ;
24562456}
24572457
2458+ #[ cfg( feature = "raw_value" ) ]
2459+ #[ test]
2460+ fn test_raw_value_from_string_unchecked ( ) {
2461+ #[ derive( Serialize ) ]
2462+ struct Wrapper {
2463+ a : i8 ,
2464+ b : Box < RawValue > ,
2465+ c : i8 ,
2466+ }
2467+
2468+ let json = r#"{"foo":[1,2,3],"bar":"\"escaped\""}"# . to_owned ( ) ;
2469+ let checked = RawValue :: from_string ( json. clone ( ) ) . unwrap ( ) ;
2470+ let unchecked = unsafe { RawValue :: from_string_unchecked ( json) } ;
2471+ assert_eq ! ( checked. get( ) , unchecked. get( ) ) ;
2472+
2473+ let wrapper = Wrapper {
2474+ a : 1 ,
2475+ b : unchecked,
2476+ c : 3 ,
2477+ } ;
2478+ let wrapper_to_string = serde_json:: to_string ( & wrapper) . unwrap ( ) ;
2479+ assert_eq ! (
2480+ r#"{"a":1,"b":{"foo":[1,2,3],"bar":"\"escaped\""},"c":3}"# ,
2481+ wrapper_to_string,
2482+ ) ;
2483+
2484+ // A string with excess capacity is preserved as-is.
2485+ let mut spare = String :: with_capacity ( 64 ) ;
2486+ spare. push_str ( "[1,2,3]" ) ;
2487+ let raw = unsafe { RawValue :: from_string_unchecked ( spare) } ;
2488+ assert_eq ! ( "[1,2,3]" , raw. get( ) ) ;
2489+ }
2490+
2491+ // The debug_assert! inside from_string_unchecked is compiled out of release
2492+ // builds, so #[should_panic] can only be exercised under debug (which is the
2493+ // default `cargo test` profile).
2494+ #[ cfg( all( feature = "raw_value" , debug_assertions) ) ]
2495+ #[ test]
2496+ #[ should_panic = "from_string_unchecked" ]
2497+ fn test_raw_value_from_string_unchecked_debug_asserts_whitespace ( ) {
2498+ let _ = unsafe { RawValue :: from_string_unchecked ( " 42" . to_owned ( ) ) } ;
2499+ }
2500+
2501+ #[ cfg( all( feature = "raw_value" , debug_assertions) ) ]
2502+ #[ test]
2503+ #[ should_panic = "from_string_unchecked" ]
2504+ fn test_raw_value_from_string_unchecked_debug_asserts_well_formed ( ) {
2505+ let _ = unsafe { RawValue :: from_string_unchecked ( "[1," . to_owned ( ) ) } ;
2506+ }
2507+
24582508#[ cfg( feature = "raw_value" ) ]
24592509#[ test]
24602510fn test_raw_invalid_utf8 ( ) {
0 commit comments