mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Add ArgFloatLike.
This commit is contained in:
@@ -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 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user