Mercurial > public > mercurial-scm > hg
comparison 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 |
comparison
equal
deleted
inserted
replaced
52610:c25d345f5aa5 | 52611:4a73eb3923ac |
---|---|
22 let owner = Bound::new(py, Owner::new("new".to_owned()))?; | 22 let owner = Bound::new(py, Owner::new("new".to_owned()))?; |
23 test(py, &owner) | 23 test(py, &owner) |
24 }) | 24 }) |
25 } | 25 } |
26 | 26 |
27 /// "leak" in the sense of `UnsafePyLeaked` the `string` data field, | 27 /// "leak" in the sense of `SharedByPyObject` the `string` data field, |
28 /// taking care of all the boilerplate | 28 /// taking care of all the boilerplate |
29 fn leak_string(owner: &Bound<'_, Owner>) -> UnsafePyLeaked<&'static String> { | 29 fn leak_string(owner: &Bound<'_, Owner>) -> SharedByPyObject<&'static String> { |
30 let cell = &owner.borrow().string; | 30 let cell = &owner.borrow().string; |
31 let shared_ref = unsafe { cell.borrow_with_owner(owner) }; | 31 let shared_ref = unsafe { cell.borrow_with_owner(owner) }; |
32 shared_ref.leak_immutable() | 32 shared_ref.share_immutable() |
33 } | 33 } |
34 | 34 |
35 fn try_leak_string( | 35 fn try_leak_string( |
36 owner: &Bound<'_, Owner>, | 36 owner: &Bound<'_, Owner>, |
37 ) -> Result<UnsafePyLeaked<&'static String>, TryLeakError> { | 37 ) -> Result<SharedByPyObject<&'static String>, TryShareError> { |
38 let cell = &owner.borrow().string; | 38 let cell = &owner.borrow().string; |
39 let shared_ref = unsafe { cell.borrow_with_owner(owner) }; | 39 let shared_ref = unsafe { cell.borrow_with_owner(owner) }; |
40 shared_ref.try_leak_immutable() | 40 shared_ref.try_share_immutable() |
41 } | 41 } |
42 | 42 |
43 /// Mutate the `string` field of `owner` as would be done from Python code | 43 /// Mutate the `string` field of `owner` as would be done from Python code |
44 /// | 44 /// |
45 /// This is to simulate normal mutation of the owner object from | 45 /// This is to simulate normal mutation of the owner object from |
102 Ok(()) | 102 Ok(()) |
103 }) | 103 }) |
104 } | 104 } |
105 | 105 |
106 #[test] | 106 #[test] |
107 #[should_panic(expected = "map() over invalidated leaked reference")] | 107 #[should_panic(expected = "map() over invalidated shared reference")] |
108 fn test_leaked_map_after_mut() { | 108 fn test_leaked_map_after_mut() { |
109 with_setup(|py, owner| { | 109 with_setup(|py, owner| { |
110 let leaked = leak_string(owner); | 110 let leaked = leak_string(owner); |
111 mutate_string(owner, String::clear); | 111 mutate_string(owner, String::clear); |
112 let _leaked_iter = unsafe { leaked.map(py, |s| s.chars()) }; | 112 let _leaked_iter = unsafe { leaked.map(py, |s| s.chars()) }; |