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