Skip to content

Commit bd92c7e

Browse files
authored
Merge pull request RustPython#2196 from qingshi163/dev_array_byteswap
Implement array byteswap()
2 parents b40965f + 78ab0a2 commit bd92c7e

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

Lib/test/test_array.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ def test_buffer_info(self):
223223
self.assertIsInstance(bi[1], int)
224224
self.assertEqual(bi[1], len(a))
225225

226-
# TODO: RUSTPYTHON
227-
@unittest.expectedFailure
228226
def test_byteswap(self):
229227
if self.typecode == 'u':
230228
example = '\U00100100'

vm/src/stdlib/array.rs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ macro_rules! def_array_enum {
334334
}
335335
}
336336

337+
fn byteswap(&mut self) {
338+
match self {
339+
$(ArrayContentType::$n(v) => {
340+
for element in v.iter_mut() {
341+
let x = element.byteswap();
342+
*element = x;
343+
}
344+
})*
345+
}
346+
}
347+
337348
fn repr(&self, _vm: &VirtualMachine) -> PyResult<String> {
338349
// we don't need ReprGuard here
339350
let s = match self {
@@ -390,31 +401,43 @@ def_array_enum!(
390401

391402
trait ArrayElement: Sized {
392403
fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>;
404+
fn byteswap(self) -> Self;
393405
}
394406

395-
macro_rules! adapt_try_into_from_object {
396-
($(($t:ty, $f:path),)*) => {$(
407+
macro_rules! impl_array_element {
408+
($(($t:ty, $f_into:path, $f_swap:path),)*) => {$(
397409
impl ArrayElement for $t {
398410
fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
399-
$f(vm, obj)
411+
$f_into(vm, obj)
412+
}
413+
fn byteswap(self) -> Self {
414+
$f_swap(self)
400415
}
401416
}
402417
)*};
403418
}
404419

405-
adapt_try_into_from_object!(
406-
(i8, i8::try_from_object),
407-
(u8, u8::try_from_object),
408-
(i16, i16::try_from_object),
409-
(u16, u16::try_from_object),
410-
(i32, i32::try_from_object),
411-
(u32, u32::try_from_object),
412-
(i64, i64::try_from_object),
413-
(u64, u64::try_from_object),
414-
(f32, f32_try_into_from_object),
415-
(f64, f64_try_into_from_object),
420+
impl_array_element!(
421+
(i8, i8::try_from_object, i8::swap_bytes),
422+
(u8, u8::try_from_object, u8::swap_bytes),
423+
(i16, i16::try_from_object, i16::swap_bytes),
424+
(u16, u16::try_from_object, u16::swap_bytes),
425+
(i32, i32::try_from_object, i32::swap_bytes),
426+
(u32, u32::try_from_object, u32::swap_bytes),
427+
(i64, i64::try_from_object, i64::swap_bytes),
428+
(u64, u64::try_from_object, u64::swap_bytes),
429+
(f32, f32_try_into_from_object, f32_swap_bytes),
430+
(f64, f64_try_into_from_object, f64_swap_bytes),
416431
);
417432

433+
fn f32_swap_bytes(x: f32) -> f32 {
434+
f32::from_bits(x.to_bits().swap_bytes())
435+
}
436+
437+
fn f64_swap_bytes(x: f64) -> f64 {
438+
f64::from_bits(x.to_bits().swap_bytes())
439+
}
440+
418441
fn f32_try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<f32> {
419442
try_float(&obj, vm)?
420443
.map(|x| x as f32)
@@ -524,6 +547,11 @@ impl PyArray {
524547
Ok(())
525548
}
526549

550+
#[pymethod]
551+
fn byteswap(&self) {
552+
self.borrow_value_mut().byteswap();
553+
}
554+
527555
#[pymethod]
528556
fn index(&self, x: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
529557
self.borrow_value().index(x, vm)

0 commit comments

Comments
 (0)