@@ -1884,22 +1884,32 @@ mod tests {
1884
1884
( 0 , old_field, ( legacy, u8 , {
1885
1885
// Sadly the type-checker needs some help
1886
1886
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
+ }
1889
1893
}
1890
1894
} , |us: & ExpandedField | Some ( us. new_field. 0 ) ) ) ,
1891
- ( 1 , new_field, required ) ,
1895
+ ( 1 , new_field, ( default_value , ( 0 , 0 ) ) ) ,
1892
1896
} ) ;
1893
1897
1894
1898
#[ test]
1895
1899
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( ) ) ;
1898
1902
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) .
1901
1905
encoded[ 3 ] = 10 ;
1902
1906
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 ) } ) ;
1904
1914
}
1905
1915
}
0 commit comments