@@ -36,8 +36,6 @@ pub enum Id {
3636 V1 ( [ u8 ; 18 ] ) ,
3737}
3838
39-
40-
4139impl Id {
4240 /// Construct V0 from uuid.
4341 pub fn new_v0 ( ) -> Self {
@@ -241,20 +239,41 @@ impl TuplePack for Id {
241239 w : & mut W ,
242240 tuple_depth : TupleDepth ,
243241 ) -> std:: io:: Result < VersionstampOffset > {
242+ let mut size = 1 ;
243+
244244 w. write_all ( & [ fdb_util:: codes:: ID ] ) ?;
245+
246+ // IMPORTANT: While the normal bytes representation of a v0 ID doesn't include the version, we write
247+ // it here so that we can unpack without a terminating NIL.
248+ if let Id :: V0 ( _) = self {
249+ w. write_all ( & [ 0 ] ) ?;
250+ size += 1 ;
251+ }
252+
245253 let bytes = self . as_bytes ( ) ;
254+
246255 let len = u32:: try_from ( bytes. len ( ) )
247256 . map_err ( |err| std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidData , err) ) ?;
257+ size += len;
258+
248259 w. write_all ( & bytes) ?;
249260
250- Ok ( VersionstampOffset :: None { size : 1 + len } )
261+ Ok ( VersionstampOffset :: None { size } )
251262 }
252263}
253264
254265impl < ' de > TupleUnpack < ' de > for Id {
255266 fn unpack ( input : & [ u8 ] , tuple_depth : TupleDepth ) -> PackResult < ( & [ u8 ] , Self ) > {
256267 let input = fdb_util:: parse_code ( input, fdb_util:: codes:: ID ) ?;
257- let ( input, slice) = fdb_util:: parse_bytes ( input, 16 ) ?;
268+ let ( input2, version) = fdb_util:: parse_byte ( input) ?;
269+
270+ let ( input, slice) = if version == 0 {
271+ // Parse 16 bytes after version
272+ fdb_util:: parse_bytes ( input2, 16 ) ?
273+ } else {
274+ // Parse 19 bytes including version
275+ fdb_util:: parse_bytes ( input, 19 ) ?
276+ } ;
258277
259278 let v = Id :: from_bytes ( slice)
260279 . map_err ( |err| PackError :: Message ( format ! ( "bad id format: {err}" ) . into ( ) ) ) ?;
0 commit comments