Skip to content

Commit a07fcd2

Browse files
committed
split convert module into sub modules
1 parent f6d70c8 commit a07fcd2

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

vm/src/convert/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod transmute_from;
2+
mod try_from;
3+
4+
pub use transmute_from::TransmuteFromObject;
5+
pub use try_from::{TryFromBorrowedObject, TryFromObject};

vm/src/convert/transmute_from.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::{
2+
pyobject::{AsPyObject, PyObject, PyRef, PyResult, PyValue},
3+
vm::VirtualMachine,
4+
};
5+
6+
/// Marks a type that has the exact same layout as PyObjectRef, e.g. a type that is
7+
/// `repr(transparent)` over PyObjectRef.
8+
///
9+
/// # Safety
10+
/// Can only be implemented for types that are `repr(transparent)` over a PyObjectRef `obj`,
11+
/// and logically valid so long as `check(vm, obj)` returns `Ok(())`
12+
pub unsafe trait TransmuteFromObject: Sized {
13+
fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()>;
14+
}
15+
16+
unsafe impl<T: PyValue> TransmuteFromObject for PyRef<T> {
17+
fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()> {
18+
let class = T::class(vm);
19+
if obj.fast_isinstance(class) {
20+
if obj.payload_is::<T>() {
21+
Ok(())
22+
} else {
23+
Err(vm.new_downcast_runtime_error(class, obj))
24+
}
25+
} else {
26+
Err(vm.new_downcast_type_error(class, obj))
27+
}
28+
}
29+
}
Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,6 @@ use crate::{
33
vm::VirtualMachine,
44
};
55

6-
/// Marks a type that has the exact same layout as PyObjectRef, e.g. a type that is
7-
/// `repr(transparent)` over PyObjectRef.
8-
///
9-
/// # Safety
10-
/// Can only be implemented for types that are `repr(transparent)` over a PyObjectRef `obj`,
11-
/// and logically valid so long as `check(vm, obj)` returns `Ok(())`
12-
pub unsafe trait TransmuteFromObject: Sized {
13-
fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()>;
14-
}
15-
16-
unsafe impl<T: PyValue> TransmuteFromObject for PyRef<T> {
17-
fn check(vm: &VirtualMachine, obj: &PyObject) -> PyResult<()> {
18-
let class = T::class(vm);
19-
if obj.fast_isinstance(class) {
20-
if obj.payload_is::<T>() {
21-
Ok(())
22-
} else {
23-
Err(vm.new_downcast_runtime_error(class, obj))
24-
}
25-
} else {
26-
Err(vm.new_downcast_type_error(class, obj))
27-
}
28-
}
29-
}
30-
316
/// Implemented by any type that can be created from a Python object.
327
///
338
/// Any type that implements `TryFromObject` is automatically `FromArgs`, and

0 commit comments

Comments
 (0)