Skip to content

Commit 9f17a50

Browse files
committed
Impl std::ops::Deref for ArgIntoBool
1 parent 6e97435 commit 9f17a50

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

vm/src/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ impl Packable for *mut raw::c_void {
566566

567567
impl Packable for bool {
568568
fn pack<E: ByteOrder>(vm: &VirtualMachine, arg: PyObjectRef, data: &mut [u8]) -> PyResult<()> {
569-
let v = ArgIntoBool::try_from_object(vm, arg)?.to_bool() as u8;
569+
let v = *ArgIntoBool::try_from_object(vm, arg)? as u8;
570570
v.pack_int::<E>(data);
571571
Ok(())
572572
}

vm/src/builtins/int.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ impl PyInt {
606606
args: IntFromByteArgs,
607607
vm: &VirtualMachine,
608608
) -> PyResult<PyRef<Self>> {
609-
let signed = args.signed.map_or(false, |s| s.to_bool());
609+
let signed = args.signed.map_or(false, Into::into);
610610
let value = match (args.byteorder.as_str(), signed) {
611611
("big", true) => BigInt::from_signed_bytes_be(&args.bytes.elements),
612612
("big", false) => BigInt::from_bytes_be(Sign::Plus, &args.bytes.elements),
@@ -623,12 +623,7 @@ impl PyInt {
623623

624624
#[pymethod]
625625
fn to_bytes(&self, args: IntToByteArgs, vm: &VirtualMachine) -> PyResult<PyBytes> {
626-
let signed = if let OptionalArg::Present(signed) = args.signed {
627-
signed.to_bool()
628-
} else {
629-
false
630-
};
631-
626+
let signed = args.signed.map_or(false, Into::into);
632627
let byte_len = args.length.try_to_primitive(vm)?;
633628

634629
let value = self.as_bigint();

vm/src/builtins/zip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl PyZip {
6161
#[pymethod(magic)]
6262
fn setstate(zelf: PyRef<Self>, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
6363
if let Ok(obj) = ArgIntoBool::try_from_object(vm, state) {
64-
zelf.strict.store(obj.to_bool(), atomic::Ordering::Release);
64+
zelf.strict.store(obj.into(), atomic::Ordering::Release);
6565
}
6666
Ok(())
6767
}

vm/src/function/number.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{AsObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine};
22
use num_complex::Complex64;
3+
use std::ops::Deref;
34

45
/// A Python complex-like object.
56
///
@@ -10,12 +11,20 @@ use num_complex::Complex64;
1011
/// method, this method will first be called to convert the object into a float.
1112
/// If `__complex__()` is not defined then it falls back to `__float__()`. If
1213
/// `__float__()` is not defined it falls back to `__index__()`.
13-
#[derive(Debug, Copy, Clone, PartialEq)]
14+
#[derive(Debug, PartialEq)]
1415
#[repr(transparent)]
1516
pub struct ArgIntoComplex {
1617
value: Complex64,
1718
}
1819

20+
impl Deref for ArgIntoComplex {
21+
type Target = Complex64;
22+
23+
fn deref(&self) -> &Self::Target {
24+
&self.value
25+
}
26+
}
27+
1928
impl ArgIntoComplex {
2029
pub fn to_complex(self) -> Complex64 {
2130
self.value
@@ -41,7 +50,7 @@ impl TryFromObject for ArgIntoComplex {
4150
/// If the object is not a Python floating point object but has a `__float__()`
4251
/// method, this method will first be called to convert the object into a float.
4352
/// If `__float__()` is not defined then it falls back to `__index__()`.
44-
#[derive(Debug, Copy, Clone, PartialEq)]
53+
#[derive(Debug, PartialEq)]
4554
#[repr(transparent)]
4655
pub struct ArgIntoFloat {
4756
value: f64,
@@ -79,23 +88,32 @@ impl TryFromObject for ArgIntoFloat {
7988
/// By default an object is considered true unless its class defines either a
8089
/// `__bool__()` method that returns False or a `__len__()` method that returns
8190
/// zero, when called with the object.
82-
#[derive(Debug, Default, Copy, Clone, PartialEq)]
91+
#[derive(Debug, Default, PartialEq)]
8392
pub struct ArgIntoBool {
8493
value: bool,
8594
}
8695

8796
impl ArgIntoBool {
88-
pub const TRUE: ArgIntoBool = ArgIntoBool { value: true };
89-
pub const FALSE: ArgIntoBool = ArgIntoBool { value: false };
97+
pub const TRUE: Self = Self { value: true };
98+
pub const FALSE: Self = Self { value: false };
99+
}
90100

91-
pub fn to_bool(self) -> bool {
92-
self.value
101+
impl From<ArgIntoBool> for bool {
102+
fn from(arg: ArgIntoBool) -> Self {
103+
arg.value
104+
}
105+
}
106+
107+
impl Deref for ArgIntoBool {
108+
type Target = bool;
109+
fn deref(&self) -> &Self::Target {
110+
&self.value
93111
}
94112
}
95113

96114
impl TryFromObject for ArgIntoBool {
97115
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
98-
Ok(ArgIntoBool {
116+
Ok(Self {
99117
value: obj.try_to_bool(vm)?,
100118
})
101119
}

vm/src/stdlib/builtins.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod builtins {
4747
#[pyfunction]
4848
fn all(iterable: ArgIterable<ArgIntoBool>, vm: &VirtualMachine) -> PyResult<bool> {
4949
for item in iterable.iter(vm)? {
50-
if !item?.to_bool() {
50+
if !*item? {
5151
return Ok(false);
5252
}
5353
}
@@ -57,7 +57,7 @@ mod builtins {
5757
#[pyfunction]
5858
fn any(iterable: ArgIterable<ArgIntoBool>, vm: &VirtualMachine) -> PyResult<bool> {
5959
for item in iterable.iter(vm)? {
60-
if item?.to_bool() {
60+
if *item? {
6161
return Ok(true);
6262
}
6363
}
@@ -671,7 +671,7 @@ mod builtins {
671671
.unwrap_or_else(|| PyStr::from("\n").into_ref(vm));
672672
write(end)?;
673673

674-
if options.flush.to_bool() {
674+
if *options.flush {
675675
vm.call_method(&file, "flush", ())?;
676676
}
677677

0 commit comments

Comments
 (0)