Add ArgFloatLike.

This commit is contained in:
jfh
2021-10-07 12:34:30 +03:00
parent acfdf9ceaa
commit 856f77fb77
8 changed files with 97 additions and 93 deletions

View File

@@ -22,3 +22,32 @@ impl TryFromObject for ArgComplexLike {
Ok(ArgComplexLike { value })
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(transparent)]
pub struct ArgFloatLike {
value: f64,
}
impl ArgFloatLike {
pub fn to_f64(self) -> f64 {
self.value
}
pub fn vec_into_f64(v: Vec<Self>) -> Vec<f64> {
// TODO: Vec::into_raw_parts once stabilized
let mut v = std::mem::ManuallyDrop::new(v);
let (p, l, c) = (v.as_mut_ptr(), v.len(), v.capacity());
// SAFETY: IntoPyFloat is repr(transparent) over f64
unsafe { Vec::from_raw_parts(p.cast(), l, c) }
}
}
impl TryFromObject for ArgFloatLike {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let value = obj.try_to_f64(vm)?.ok_or_else(|| {
vm.new_type_error(format!("must be real number, not {}", obj.class().name()))
})?;
Ok(ArgFloatLike { value })
}
}