use super::argument::OptionalArg;
use crate::{AsObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, builtins::PyIntRef};
use core::ops::Deref;
use malachite_bigint::BigInt;
use num_complex::Complex64;
use num_traits::PrimInt;
#[derive(Debug, PartialEq)]
#[repr(transparent)]
pub struct ArgIntoComplex {
value: Complex64,
}
impl ArgIntoComplex {
#[inline]
pub fn into_complex(self) -> Complex64 {
self.value
}
}
impl From<ArgIntoComplex> for Complex64 {
fn from(arg: ArgIntoComplex) -> Self {
arg.value
}
}
impl TryFromObject for ArgIntoComplex {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let (value, _) = obj.try_complex(vm)?.ok_or_else(|| {
vm.new_type_error(format!("must be real number, not {}", obj.class().name()))
})?;
Ok(Self { value })
}
}
#[derive(Debug, PartialEq)]
#[repr(transparent)]
pub struct ArgIntoFloat {
value: f64,
}
impl ArgIntoFloat {
#[inline]
pub fn into_float(self) -> f64 {
self.value
}
pub fn vec_into_f64(v: Vec<Self>) -> Vec<f64> {
let mut v = core::mem::ManuallyDrop::new(v);
let (p, l, c) = (v.as_mut_ptr(), v.len(), v.capacity());
unsafe { Vec::from_raw_parts(p.cast(), l, c) }
}
}
impl From<ArgIntoFloat> for f64 {
fn from(arg: ArgIntoFloat) -> Self {
arg.value
}
}
impl TryFromObject for ArgIntoFloat {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let value = obj.try_float(vm)?.to_f64();
Ok(Self { value })
}
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ArgIntoBool {
value: bool,
}
impl ArgIntoBool {
pub const TRUE: Self = Self { value: true };
pub const FALSE: Self = Self { value: false };
#[inline]
pub fn into_bool(self) -> bool {
self.value
}
}
impl From<ArgIntoBool> for bool {
fn from(arg: ArgIntoBool) -> Self {
arg.value
}
}
impl TryFromObject for ArgIntoBool {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
Ok(Self {
value: obj.try_to_bool(vm)?,
})
}
}
#[derive(Debug, Traverse)]
#[repr(transparent)]
pub struct ArgIndex {
value: PyIntRef,
}
impl ArgIndex {
#[inline]
pub fn into_int_ref(self) -> PyIntRef {
self.value
}
}
impl AsRef<PyIntRef> for ArgIndex {
fn as_ref(&self) -> &PyIntRef {
&self.value
}
}
impl From<ArgIndex> for PyIntRef {
fn from(arg: ArgIndex) -> Self {
arg.value
}
}
impl TryFromObject for ArgIndex {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
Ok(Self {
value: obj.try_index(vm)?,
})
}
}
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct ArgPrimitiveIndex<T> {
pub value: T,
}
impl<T> OptionalArg<ArgPrimitiveIndex<T>> {
pub fn into_primitive(self) -> OptionalArg<T> {
self.map(|x| x.value)
}
}
impl<T> Deref for ArgPrimitiveIndex<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T> TryFromObject for ArgPrimitiveIndex<T>
where
T: PrimInt + for<'a> TryFrom<&'a BigInt>,
{
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
Ok(Self {
value: obj.try_index(vm)?.try_to_primitive(vm)?,
})
}
}
pub type ArgSize = ArgPrimitiveIndex<isize>;
impl From<ArgSize> for isize {
fn from(arg: ArgSize) -> Self {
arg.value
}
}