@@ -1862,22 +1862,32 @@ mod tests {
1862
1862
( 0 , old_field, ( legacy, u8 , {
1863
1863
// Sadly the type-checker needs some help
1864
1864
let _: & ( u8 , u8 ) = & new_field;
1865
- if let Some ( old_field) = old_field {
1866
- new_field. 0 = old_field;
1865
+ // If new_field.0 is 0, we assume we hit the default_value case below and overwrite the
1866
+ // value with the old data.
1867
+ if new_field == ( 0 , 0 ) {
1868
+ if let Some ( old_field) = old_field {
1869
+ new_field. 0 = old_field;
1870
+ }
1867
1871
}
1868
1872
} , |us: & ExpandedField | Some ( us. new_field. 0 ) ) ) ,
1869
- ( 1 , new_field, required ) ,
1873
+ ( 1 , new_field, ( default_value , ( 0 , 0 ) ) ) ,
1870
1874
} ) ;
1871
1875
1872
1876
#[ test]
1873
1877
fn test_legacy_conversion ( ) {
1874
- let mut encoded = ExpandedField { new_field : ( 42 , 43 ) } . encode ( ) ;
1875
- assert_eq ! ( encoded, <Vec <u8 >>:: from_hex( "0700012a01022a2b " ) . unwrap( ) ) ;
1878
+ let mut encoded = ExpandedField { new_field : ( 43 , 42 ) } . encode ( ) ;
1879
+ assert_eq ! ( encoded, <Vec <u8 >>:: from_hex( "0700012b01022b2a " ) . unwrap( ) ) ;
1876
1880
1877
- // On read, the post- read action will run, using the old_field value to overwrite the
1878
- // new_field .
1881
+ // On read, we'll read the `new_field` and have a value other than `(0, 0)`, causing us to
1882
+ // ignore the old field value (in byte 3) .
1879
1883
encoded[ 3 ] = 10 ;
1880
1884
let read = <ExpandedField as Readable >:: read ( & mut & encoded[ ..] ) . unwrap ( ) ;
1881
- assert_eq ! ( read, ExpandedField { new_field: ( 10 , 43 ) } ) ;
1885
+ assert_eq ! ( read, ExpandedField { new_field: ( 43 , 42 ) } ) ;
1886
+
1887
+ // On read, if we read an old `ExpandedField` that just has a type-0 `old_field` entry,
1888
+ // we'll copy that into the first position of `new_field`.
1889
+ let encoded = <Vec < u8 > >:: from_hex ( "0300012a" ) . unwrap ( ) ;
1890
+ let read = <ExpandedField as Readable >:: read ( & mut & encoded[ ..] ) . unwrap ( ) ;
1891
+ assert_eq ! ( read, ExpandedField { new_field: ( 42 , 0 ) } ) ;
1882
1892
}
1883
1893
}
0 commit comments