@@ -5,6 +5,7 @@ use super::{
55} ;
66use crate :: {
77 anystr:: { self , AnyStr } ,
8+ atomic_func,
89 byte:: { bytes_from_object, value_from_object} ,
910 bytesinner:: {
1011 bytes_decode, ByteInnerFindOptions , ByteInnerNewOptions , ByteInnerPaddingOptions ,
@@ -784,47 +785,51 @@ impl AsMapping for PyByteArray {
784785}
785786
786787impl AsSequence for PyByteArray {
787- const AS_SEQUENCE : PySequenceMethods = PySequenceMethods {
788- length : Some ( |seq, _vm| Ok ( Self :: sequence_downcast ( seq) . len ( ) ) ) ,
789- concat : Some ( |seq, other, vm| {
790- Self :: sequence_downcast ( seq)
791- . inner ( )
792- . concat ( other, vm)
793- . map ( |x| PyByteArray :: from ( x) . into_pyobject ( vm) )
794- } ) ,
795- repeat : Some ( |seq, n, vm| {
796- Self :: sequence_downcast ( seq)
797- . mul ( n as isize , vm)
798- . map ( |x| x. into_pyobject ( vm) )
799- } ) ,
800- item : Some ( |seq, i, vm| {
801- Self :: sequence_downcast ( seq)
802- . borrow_buf ( )
803- . getitem_by_index ( vm, i)
804- . map ( |x| vm. ctx . new_bytes ( vec ! [ x] ) . into ( ) )
805- } ) ,
806- ass_item : Some ( |seq, i, value, vm| {
807- let zelf = Self :: sequence_downcast ( seq) ;
808- if let Some ( value) = value {
809- zelf. _setitem_by_index ( i, value, vm)
810- } else {
811- zelf. borrow_buf_mut ( ) . del_item_by_index ( vm, i)
812- }
813- } ) ,
814- contains : Some ( |seq, other, vm| {
815- let other = <Either < PyBytesInner , PyIntRef > >:: try_from_object ( vm, other. to_owned ( ) ) ?;
816- Self :: sequence_downcast ( seq) . contains ( other, vm)
817- } ) ,
818- inplace_concat : Some ( |seq, other, vm| {
819- let other = ArgBytesLike :: try_from_object ( vm, other. to_owned ( ) ) ?;
820- let zelf = Self :: sequence_downcast ( seq) . to_owned ( ) ;
821- Self :: iadd ( zelf, other, vm) . map ( |x| x. into ( ) )
822- } ) ,
823- inplace_repeat : Some ( |seq, n, vm| {
824- let zelf = Self :: sequence_downcast ( seq) . to_owned ( ) ;
825- Self :: imul ( zelf, n as isize , vm) . map ( |x| x. into ( ) )
826- } ) ,
827- } ;
788+ fn as_sequence ( ) -> & ' static PySequenceMethods {
789+ static AS_SEQUENCE : PySequenceMethods = PySequenceMethods {
790+ length : atomic_func ! ( |seq, _vm| Ok ( PyByteArray :: sequence_downcast( seq) . len( ) ) ) ,
791+ concat : atomic_func ! ( |seq, other, vm| {
792+ PyByteArray :: sequence_downcast( seq)
793+ . inner( )
794+ . concat( other, vm)
795+ . map( |x| PyByteArray :: from( x) . into_pyobject( vm) )
796+ } ) ,
797+ repeat : atomic_func ! ( |seq, n, vm| {
798+ PyByteArray :: sequence_downcast( seq)
799+ . mul( n as isize , vm)
800+ . map( |x| x. into_pyobject( vm) )
801+ } ) ,
802+ item : atomic_func ! ( |seq, i, vm| {
803+ PyByteArray :: sequence_downcast( seq)
804+ . borrow_buf( )
805+ . getitem_by_index( vm, i)
806+ . map( |x| vm. ctx. new_bytes( vec![ x] ) . into( ) )
807+ } ) ,
808+ ass_item : atomic_func ! ( |seq, i, value, vm| {
809+ let zelf = PyByteArray :: sequence_downcast( seq) ;
810+ if let Some ( value) = value {
811+ zelf. _setitem_by_index( i, value, vm)
812+ } else {
813+ zelf. borrow_buf_mut( ) . del_item_by_index( vm, i)
814+ }
815+ } ) ,
816+ contains : atomic_func ! ( |seq, other, vm| {
817+ let other =
818+ <Either <PyBytesInner , PyIntRef >>:: try_from_object( vm, other. to_owned( ) ) ?;
819+ PyByteArray :: sequence_downcast( seq) . contains( other, vm)
820+ } ) ,
821+ inplace_concat : atomic_func ! ( |seq, other, vm| {
822+ let other = ArgBytesLike :: try_from_object( vm, other. to_owned( ) ) ?;
823+ let zelf = PyByteArray :: sequence_downcast( seq) . to_owned( ) ;
824+ PyByteArray :: iadd( zelf, other, vm) . map( |x| x. into( ) )
825+ } ) ,
826+ inplace_repeat : atomic_func ! ( |seq, n, vm| {
827+ let zelf = PyByteArray :: sequence_downcast( seq) . to_owned( ) ;
828+ PyByteArray :: imul( zelf, n as isize , vm) . map( |x| x. into( ) )
829+ } ) ,
830+ } ;
831+ & AS_SEQUENCE
832+ }
828833}
829834
830835impl Unhashable for PyByteArray { }
0 commit comments