Mercurial > public > mercurial-scm > hg
diff rust/pyo3-sharedref/tests/test_sharedref.rs @ 52611:4a73eb3923ac
rust-pyo3-sharedref: renamed UnsafePyLeaked to SharedByPyObject
Rationale:
- the object itself is not unsafe, only most of its methods are. This
is similar to raw pointers: obtaining them is not unsafe, using
them is. That being said, we are not ready yet to declare, e.g,
`borrow_with_owner()` safe because that gives an early warning to
users.
- it was not really a leak, as the data is actually owned by a
Python object on which we keep a (Python) reference with proper
increment of the refcount. Hence the terminology was too much of
a deterrent.
We hope that the new terminology conveys both that the data
contains a shared reference and that it was generously lended
by a Python object.
The `SharedByPyObjectRef` name is arguably heavy, but it is merely
an implementation detail that users will not be much exposed to,
thanks to the `Deref` implementation.
As previously, we keep the changes in tests to a minimum, to make
obvious in the diff that the tests have not changed. We will
take care of renaming their local variables and functions in a later
move.
author | Georges Racinet <georges.racinet@cloudcrane.io> |
---|---|
date | Sun, 15 Dec 2024 15:19:43 +0100 |
parents | c25d345f5aa5 |
children | a945845137b1 |
line wrap: on
line diff
--- a/rust/pyo3-sharedref/tests/test_sharedref.rs Sun Dec 15 15:03:27 2024 +0100 +++ b/rust/pyo3-sharedref/tests/test_sharedref.rs Sun Dec 15 15:19:43 2024 +0100 @@ -24,20 +24,20 @@ }) } -/// "leak" in the sense of `UnsafePyLeaked` the `string` data field, +/// "leak" in the sense of `SharedByPyObject` the `string` data field, /// taking care of all the boilerplate -fn leak_string(owner: &Bound<'_, Owner>) -> UnsafePyLeaked<&'static String> { +fn leak_string(owner: &Bound<'_, Owner>) -> SharedByPyObject<&'static String> { let cell = &owner.borrow().string; let shared_ref = unsafe { cell.borrow_with_owner(owner) }; - shared_ref.leak_immutable() + shared_ref.share_immutable() } fn try_leak_string( owner: &Bound<'_, Owner>, -) -> Result<UnsafePyLeaked<&'static String>, TryLeakError> { +) -> Result<SharedByPyObject<&'static String>, TryShareError> { let cell = &owner.borrow().string; let shared_ref = unsafe { cell.borrow_with_owner(owner) }; - shared_ref.try_leak_immutable() + shared_ref.try_share_immutable() } /// Mutate the `string` field of `owner` as would be done from Python code @@ -104,7 +104,7 @@ } #[test] -#[should_panic(expected = "map() over invalidated leaked reference")] +#[should_panic(expected = "map() over invalidated shared reference")] fn test_leaked_map_after_mut() { with_setup(|py, owner| { let leaked = leak_string(owner);