@@ -7,9 +7,7 @@ use crate::inspect::TypeHint;
77#[ cfg( feature = "experimental-inspect" ) ]
88use crate :: type_object:: PyTypeInfo ;
99use crate :: {
10- conversion:: IntoPyObject ,
11- types:: { PyByteArray , PyByteArrayMethods , PyBytes } ,
12- Bound , CastError , PyAny , PyErr , Python ,
10+ conversion:: IntoPyObject , types:: PyBytes , Bound , CastError , PyAny , PyErr , PyResult , Python ,
1311} ;
1412
1513impl < ' a , ' py , T > IntoPyObject < ' py > for & ' a [ T ]
@@ -63,18 +61,17 @@ impl<'a, 'py> crate::conversion::FromPyObject<'a, 'py> for &'a [u8] {
6361/// pointing into the source object, and no copying or heap allocations will happen.
6462/// If it is a `bytearray`, its contents will be copied to an owned `Cow`.
6563impl < ' a , ' py > crate :: conversion:: FromPyObject < ' a , ' py > for Cow < ' a , [ u8 ] > {
66- type Error = CastError < ' a , ' py > ;
64+ type Error = PyErr ;
6765
6866 #[ cfg( feature = "experimental-inspect" ) ]
69- const INPUT_TYPE : TypeHint = TypeHint :: union ( & [ PyBytes :: TYPE_HINT , PyByteArray :: TYPE_HINT ] ) ;
70-
71- fn extract ( ob : crate :: Borrowed < ' a , ' py , PyAny > ) -> Result < Self , Self :: Error > {
72- if let Ok ( bytes) = ob. cast :: < PyBytes > ( ) {
73- return Ok ( Cow :: Borrowed ( bytes. as_bytes ( ) ) ) ;
74- }
75-
76- let byte_array = ob. cast :: < PyByteArray > ( ) ?;
77- Ok ( Cow :: Owned ( byte_array. to_vec ( ) ) )
67+ const INPUT_TYPE : TypeHint = Vec :: < u8 > :: INPUT_TYPE ;
68+
69+ fn extract ( ob : crate :: Borrowed < ' a , ' py , PyAny > ) -> PyResult < Self > {
70+ Ok ( if let Ok ( bytes) = ob. cast :: < PyBytes > ( ) {
71+ Cow :: Borrowed ( bytes. as_bytes ( ) ) // It's immutable, we can take a slice
72+ } else {
73+ Cow :: Owned ( Vec :: extract ( ob) ?) // Not possible to take a slice, we have to build a Vec<u8>
74+ } )
7875 }
7976
8077 #[ cfg( feature = "experimental-inspect" ) ]
0 commit comments