1+ use std:: cell:: RefCell ;
2+ use std:: hash:: { Hash , Hasher } ;
3+ use std:: ops:: Deref ;
4+ use std:: ops:: DerefMut ;
5+
16use super :: objint;
27use super :: objtype;
38use crate :: pyobject:: {
49 PyContext , PyFuncArgs , PyObject , PyObjectPayload , PyObjectRef , PyResult , TypeProtocol ,
510} ;
611use crate :: vm:: VirtualMachine ;
712use num_traits:: ToPrimitive ;
8- use std:: cell:: Ref ;
9- use std:: cell:: RefMut ;
10- use std:: hash:: { Hash , Hasher } ;
11- use std:: ops:: Deref ;
12- use std:: ops:: DerefMut ;
1313
1414// Binary data support
1515
@@ -70,7 +70,12 @@ fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7070 vec ! [ ]
7171 } ;
7272
73- Ok ( PyObject :: new ( PyObjectPayload :: Bytes { value } , cls. clone ( ) ) )
73+ Ok ( PyObject :: new (
74+ PyObjectPayload :: Bytes {
75+ value : RefCell :: new ( value) ,
76+ } ,
77+ cls. clone ( ) ,
78+ ) )
7479}
7580
7681fn bytes_eq ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
@@ -98,11 +103,7 @@ fn bytes_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
98103 let result = if objtype:: isinstance ( b, & vm. ctx . bytes_type ( ) ) {
99104 get_value ( a) . to_vec ( ) >= get_value ( b) . to_vec ( )
100105 } else {
101- return Err ( vm. new_type_error ( format ! (
102- "Cannot compare {} and {} using '>'" ,
103- a. borrow( ) ,
104- b. borrow( )
105- ) ) ) ;
106+ return Err ( vm. new_type_error ( format ! ( "Cannot compare {} and {} using '>'" , a, b) ) ) ;
106107 } ;
107108 Ok ( vm. ctx . new_bool ( result) )
108109}
@@ -117,11 +118,7 @@ fn bytes_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
117118 let result = if objtype:: isinstance ( b, & vm. ctx . bytes_type ( ) ) {
118119 get_value ( a) . to_vec ( ) > get_value ( b) . to_vec ( )
119120 } else {
120- return Err ( vm. new_type_error ( format ! (
121- "Cannot compare {} and {} using '>='" ,
122- a. borrow( ) ,
123- b. borrow( )
124- ) ) ) ;
121+ return Err ( vm. new_type_error ( format ! ( "Cannot compare {} and {} using '>='" , a, b) ) ) ;
125122 } ;
126123 Ok ( vm. ctx . new_bool ( result) )
127124}
@@ -136,11 +133,7 @@ fn bytes_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
136133 let result = if objtype:: isinstance ( b, & vm. ctx . bytes_type ( ) ) {
137134 get_value ( a) . to_vec ( ) <= get_value ( b) . to_vec ( )
138135 } else {
139- return Err ( vm. new_type_error ( format ! (
140- "Cannot compare {} and {} using '<'" ,
141- a. borrow( ) ,
142- b. borrow( )
143- ) ) ) ;
136+ return Err ( vm. new_type_error ( format ! ( "Cannot compare {} and {} using '<'" , a, b) ) ) ;
144137 } ;
145138 Ok ( vm. ctx . new_bool ( result) )
146139}
@@ -155,11 +148,7 @@ fn bytes_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
155148 let result = if objtype:: isinstance ( b, & vm. ctx . bytes_type ( ) ) {
156149 get_value ( a) . to_vec ( ) < get_value ( b) . to_vec ( )
157150 } else {
158- return Err ( vm. new_type_error ( format ! (
159- "Cannot compare {} and {} using '<='" ,
160- a. borrow( ) ,
161- b. borrow( )
162- ) ) ) ;
151+ return Err ( vm. new_type_error ( format ! ( "Cannot compare {} and {} using '<='" , a, b) ) ) ;
163152 } ;
164153 Ok ( vm. ctx . new_bool ( result) )
165154}
@@ -181,23 +170,19 @@ fn bytes_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
181170}
182171
183172pub fn get_value < ' a > ( obj : & ' a PyObjectRef ) -> impl Deref < Target = Vec < u8 > > + ' a {
184- Ref :: map ( obj. borrow ( ) , |py_obj| {
185- if let PyObjectPayload :: Bytes { ref value } = py_obj. payload {
186- value
187- } else {
188- panic ! ( "Inner error getting bytearray {:?}" , obj) ;
189- }
190- } )
173+ if let PyObjectPayload :: Bytes { ref value } = obj. payload {
174+ value. borrow ( )
175+ } else {
176+ panic ! ( "Inner error getting bytearray {:?}" , obj) ;
177+ }
191178}
192179
193180pub fn get_mut_value < ' a > ( obj : & ' a PyObjectRef ) -> impl DerefMut < Target = Vec < u8 > > + ' a {
194- RefMut :: map ( obj. borrow_mut ( ) , |py_obj| {
195- if let PyObjectPayload :: Bytes { ref mut value } = py_obj. payload {
196- value
197- } else {
198- panic ! ( "Inner error getting bytearray {:?}" , obj) ;
199- }
200- } )
181+ if let PyObjectPayload :: Bytes { ref value } = obj. payload {
182+ value. borrow_mut ( )
183+ } else {
184+ panic ! ( "Inner error getting bytearray {:?}" , obj) ;
185+ }
201186}
202187
203188fn bytes_repr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
0 commit comments