@@ -732,11 +732,38 @@ impl<C: Constant> CodeObject<C> {
732732 }
733733}
734734
735+ #[ derive( Debug ) ]
736+ #[ non_exhaustive]
737+ pub enum CodeDeserializeError {
738+ Eof ,
739+ Other ,
740+ }
741+ impl fmt:: Display for CodeDeserializeError {
742+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
743+ match self {
744+ Self :: Eof => f. write_str ( "unexpected end of data" ) ,
745+ Self :: Other => f. write_str ( "invalid bytecode" ) ,
746+ }
747+ }
748+ }
749+ impl std:: error:: Error for CodeDeserializeError { }
750+
735751impl CodeObject < ConstantData > {
736752 /// Load a code object from bytes
737- pub fn from_bytes ( data : & [ u8 ] ) -> Result < Self , Box < dyn std:: error:: Error > > {
738- let raw_bincode = lz4_flex:: decompress_size_prepended ( data) ?;
739- let data = bincode:: deserialize ( & raw_bincode) ?;
753+ pub fn from_bytes ( data : & [ u8 ] ) -> Result < Self , CodeDeserializeError > {
754+ use lz4_flex:: block:: DecompressError ;
755+ let raw_bincode = lz4_flex:: decompress_size_prepended ( data) . map_err ( |e| match e {
756+ DecompressError :: OutputTooSmall { .. } | DecompressError :: ExpectedAnotherByte => {
757+ CodeDeserializeError :: Eof
758+ }
759+ _ => CodeDeserializeError :: Other ,
760+ } ) ?;
761+ let data = bincode:: deserialize ( & raw_bincode) . map_err ( |e| match * e {
762+ bincode:: ErrorKind :: Io ( e) if e. kind ( ) == std:: io:: ErrorKind :: UnexpectedEof => {
763+ CodeDeserializeError :: Eof
764+ }
765+ _ => CodeDeserializeError :: Other ,
766+ } ) ?;
740767 Ok ( data)
741768 }
742769
0 commit comments