assert-unmoved
A type that asserts that the underlying type is not moved after being pinned and mutably accessed.
This is a rewrite of futures-test's AssertUnmoved
to allow use in more
use cases. This also supports traits other than futures.
Many of the changes made in this project are also reflected upstream: rust-lang/futures-rs#2148, rust-lang/futures-rs#2208
Usage
Add this to your Cargo.toml
:
[]
= "0.1"
Compiler support: requires rustc 1.37+
Examples
An example of using Pin::new_unchecked
incorrectly (should panic):
use ;
use AssertUnmoved;
use Pin;
let waker = noop_waker;
let mut cx = from_waker;
// First we allocate the future on the stack and poll it.
let mut future = new;
let pinned_future = unsafe ;
assert!;
// Next we move it back to the heap and poll it again. This second call
// should panic (as the future is moved).
let mut boxed_future = Box new;
let pinned_boxed_future = unsafe ;
let _ = pinned_boxed_future.poll.is_pending;
An example of incorrect StreamExt::next
implementation (should panic):
use ;
use AssertUnmoved;
use Pin;
;
let waker = noop_waker;
let mut cx = from_waker;
let mut stream = new;
// Move stream to the heap.
let mut boxed_stream = Box pin;
let next = Next;
let mut pinned_next = Box pin;
// This should panic (as the future is moved).
let _ = pinned_next.as_mut.poll.is_pending;
Optional features
futures03
— Implements futures v0.3 traits for assert-unmoved types.tokio1
— Implements tokio v1 traits for assert-unmoved types.tokio03
— Implements tokio v0.3 traits for assert-unmoved types.tokio02
— Implements tokio v0.2 traits for assert-unmoved types.
Related Projects
- pin-project: A crate for safe and ergonomic pin-projection.
- pin-project-lite: A lightweight version of pin-project written with declarative macros.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.