@@ -1884,22 +1884,32 @@ mod tests {
18841884 ( 0 , old_field, ( legacy, u8 , {
18851885 // Sadly the type-checker needs some help
18861886 let _: & ( u8 , u8 ) = & new_field;
1887- if let Some ( old_field) = old_field {
1888- new_field. 0 = old_field;
1887+ // If new_field.0 is 0, we assume we hit the default_value case below and overwrite the
1888+ // value with the old data.
1889+ if new_field == ( 0 , 0 ) {
1890+ if let Some ( old_field) = old_field {
1891+ new_field. 0 = old_field;
1892+ }
18891893 }
18901894 } , |us: & ExpandedField | Some ( us. new_field. 0 ) ) ) ,
1891- ( 1 , new_field, required ) ,
1895+ ( 1 , new_field, ( default_value , ( 0 , 0 ) ) ) ,
18921896 } ) ;
18931897
18941898 #[ test]
18951899 fn test_legacy_conversion ( ) {
1896- let mut encoded = ExpandedField { new_field : ( 42 , 43 ) } . encode ( ) ;
1897- assert_eq ! ( encoded, <Vec <u8 >>:: from_hex( "0700012a01022a2b " ) . unwrap( ) ) ;
1900+ let mut encoded = ExpandedField { new_field : ( 43 , 42 ) } . encode ( ) ;
1901+ assert_eq ! ( encoded, <Vec <u8 >>:: from_hex( "0700012b01022b2a " ) . unwrap( ) ) ;
18981902
1899- // On read, the post- read action will run, using the old_field value to overwrite the
1900- // new_field .
1903+ // On read, we'll read the `new_field` and have a value other than `(0, 0)`, causing us to
1904+ // ignore the old field value (in byte 3) .
19011905 encoded[ 3 ] = 10 ;
19021906 let read = <ExpandedField as Readable >:: read ( & mut & encoded[ ..] ) . unwrap ( ) ;
1903- assert_eq ! ( read, ExpandedField { new_field: ( 10 , 43 ) } ) ;
1907+ assert_eq ! ( read, ExpandedField { new_field: ( 43 , 42 ) } ) ;
1908+
1909+ // On read, if we read an old `ExpandedField` that just has a type-0 `old_field` entry,
1910+ // we'll copy that into the first position of `new_field`.
1911+ let encoded = <Vec < u8 > >:: from_hex ( "0300012a" ) . unwrap ( ) ;
1912+ let read = <ExpandedField as Readable >:: read ( & mut & encoded[ ..] ) . unwrap ( ) ;
1913+ assert_eq ! ( read, ExpandedField { new_field: ( 42 , 0 ) } ) ;
19041914 }
19051915}
0 commit comments