https://github.com/slint-ui/pin-weak
Small wrapper around an equivalent of Pin<Weak<T>>
https://github.com/slint-ui/pin-weak
Last synced: about 1 month ago
JSON representation
Small wrapper around an equivalent of Pin<Weak<T>>
- Host: GitHub
- URL: https://github.com/slint-ui/pin-weak
- Owner: slint-ui
- License: mit
- Created: 2020-07-31T16:46:09.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-07-21T15:15:04.000Z (over 3 years ago)
- Last Synced: 2025-05-27T00:19:09.064Z (8 months ago)
- Language: Rust
- Size: 12.7 KB
- Stars: 8
- Watchers: 4
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://crates.io/crates/pin-weak)
[](https://docs.rs/pin-weak/)
# pin-weak
This create provides weak pointers for `Pin>` and `Pin>`
### Motivation
`Pin>` and `Pin>` cannot be converted safely to
their `Weak` equivalent if `T` does not implement `Unpin`.
That's because it would otherwise be possible to do something like this:
```rust
struct SomeStruct(PhantomPinned);
let pinned = Rc::pin(SomeStruct(PhantomPinned));
// This is unsafe ...
let weak = unsafe {
Rc::downgrade(&Pin::into_inner_unchecked(pinned.clone()))
};
// ... because otherwise it would be possible to move the content of pinned:
let mut unpinned_rc = weak.upgrade().unwrap();
std::mem::drop((pinned, weak));
// unpinned_rc is now the only reference so this will work:
let x = std::mem::replace(
Rc::get_mut(&mut unpinned_rc).unwrap(),
SomeStruct(PhantomPinned),
);
```
In that example, `x` is the original `SomeStruct` which we moved in memory,
**that is undefined behavior**, do not do that at home.
### `PinWeak`
This crate simply provide a `rc::PinWeak` and `sync::PinWeak` which allow to
get weak pointer from `Pin` and `Pin`.
This is safe because you can one can only get back a `Pin` out of it when
trying to upgrade the weak pointer.
`PinWeak` can be created using the `PinWeak` downgrade function.
### Example
```rust
use pin_weak::rc::*;
struct SomeStruct(PhantomPinned, usize);
let pinned = Rc::pin(SomeStruct(PhantomPinned, 42));
let weak = PinWeak::downgrade(pinned.clone());
assert_eq!(weak.upgrade().unwrap().1, 42);
std::mem::drop(pinned);
assert!(weak.upgrade().is_none());
```
## License
MIT