@@ -1870,4 +1870,33 @@ mod tests {
18701870 assert_eq ! ( TuplesOnly :: read( & mut none_data_read) . unwrap( ) , None ) ;
18711871 assert_eq ! ( none_data_read. position( ) , unknown_data_variant. len( ) as u64 ) ;
18721872 }
1873+
1874+ #[ derive( Debug , PartialEq , Eq ) ]
1875+ struct ExpandedField {
1876+ // Old versions of LDK are presumed to have had something like:
1877+ // old_field: u8,
1878+ new_field : ( u8 , u8 ) ,
1879+ }
1880+ impl_writeable_tlv_based ! ( ExpandedField , {
1881+ ( 0 , old_field, ( legacy, u8 , {
1882+ // Sadly the type-checker needs some help
1883+ let _: & ( u8 , u8 ) = & new_field;
1884+ if let Some ( old_field) = old_field {
1885+ new_field. 0 = old_field;
1886+ }
1887+ } , |us: & ExpandedField | Some ( us. new_field. 0 ) ) ) ,
1888+ ( 1 , new_field, required) ,
1889+ } ) ;
1890+
1891+ #[ test]
1892+ fn test_legacy_conversion ( ) {
1893+ let mut encoded = ExpandedField { new_field : ( 42 , 43 ) } . encode ( ) ;
1894+ assert_eq ! ( encoded, <Vec <u8 >>:: from_hex( "0700012a01022a2b" ) . unwrap( ) ) ;
1895+
1896+ // On read, the post-read action will run, using the old_field value to overwrite the
1897+ // new_field.
1898+ encoded[ 3 ] = 10 ;
1899+ let read = <ExpandedField as Readable >:: read ( & mut & encoded[ ..] ) . unwrap ( ) ;
1900+ assert_eq ! ( read, ExpandedField { new_field: ( 10 , 43 ) } ) ;
1901+ }
18731902}
0 commit comments