@@ -3,7 +3,7 @@ use mimalloc::MiMalloc;
33#[ global_allocator]
44static GLOBAL : MiMalloc = MiMalloc ;
55
6- use std:: io:: { self , Write } ;
6+ use std:: io:: { self , Cursor , Write } ;
77
88use anyhow:: { anyhow, Result } ;
99use cbor4ii:: core:: {
@@ -504,7 +504,10 @@ pub fn decode_car<'py>(py: Python<'py>, data: &[u8]) -> PyResult<(Py<PyAny>, Bou
504504 break ;
505505 }
506506
507- let cid_result = Cid :: read_bytes ( & mut buf. buf ) ;
507+ let cid_bytes_before = buf. buf ;
508+ let mut cursor = Cursor :: new ( cid_bytes_before) ;
509+
510+ let cid_result = Cid :: read_bytes ( & mut cursor) ;
508511 let Ok ( cid) = cid_result else {
509512 return Err ( get_err (
510513 "Failed to read CID of block" ,
@@ -519,6 +522,10 @@ pub fn decode_car<'py>(py: Python<'py>, data: &[u8]) -> PyResult<(Py<PyAny>, Bou
519522 ) ) ;
520523 }
521524
525+ let consumed = cursor. position ( ) as usize ;
526+ buf. advance ( consumed) ;
527+ let cid_raw = & cid_bytes_before[ ..consumed] ;
528+
522529 let block_result = decode_dag_cbor_to_pyobject ( py, buf, 0 ) ;
523530 let Ok ( block) = block_result else {
524531 return Err ( get_err (
@@ -527,8 +534,7 @@ pub fn decode_car<'py>(py: Python<'py>, data: &[u8]) -> PyResult<(Py<PyAny>, Bou
527534 ) ) ;
528535 } ;
529536
530- // FIXME(MarshalX): to_bytes allocates
531- let key = PyBytes :: new ( py, & cid. to_bytes ( ) ) . into_pyobject ( py) ?;
537+ let key = PyBytes :: new ( py, cid_raw) . into_pyobject ( py) ?;
532538 parsed_blocks. set_item ( key, block) ?;
533539 }
534540
0 commit comments