pub struct Buffered<'a, T: ?Sized>(/* private fields */);
Expand description
A pointer to objects stored in buffers.
Containers such as &mut [u8]
or &mut Vec<u8>
yield this pointer type.
Note that, unlike most pointer types, it implements Unpin
only if T
is
Unpin
. While this may seem counterintuitive, it simplifies obtaining a
pinned reference to T
in safe Rust, as illustrated below:
fn async_hello() -> Fn!(=> dyn Future<Output = String>) {
from_fn!(|| async { String::from("Hello!") })
}
let mut stack = MaybeUninit::<[u8; 16]>::uninit();
let fut: Buffered<dyn Future<Output = String>> = async_hello().init(&mut stack);
// Pin it on the stack just as it has the inner type `T = dyn Future`.
let fut: Pin<&mut Buffered<_>> = std::pin::pin!(fut);
// Then project it to obtain a pinned reference to `T`.
let fut: Pin<&mut dyn Future<Output = String>> = fut.project();
assert_eq!(fut.await, "Hello!");
Tips: Buffered<T: Future>
implements Future
, so you can simply write
async_hello().init(&mut stack).await
in practice.
Implementations§
Source§impl<'a, T: ?Sized> Buffered<'a, T>
impl<'a, T: ?Sized> Buffered<'a, T>
Sourcepub unsafe fn from_raw(ptr: NonNull<T>) -> Self
pub unsafe fn from_raw(ptr: NonNull<T>) -> Self
Constructs a new instance with the provided pointer.
§Safety
ptr
must be a valid pointer to T
and exclusive for the returned
instance.
Sourcepub fn project(self: Pin<&mut Self>) -> Pin<&mut T>
pub fn project(self: Pin<&mut Self>) -> Pin<&mut T>
Returns a pinned mutable reference to the inner value.
Sourcepub fn project_ref(self: Pin<&Self>) -> Pin<&T>
pub fn project_ref(self: Pin<&Self>) -> Pin<&T>
Returns a pinned immutable reference to the inner value.
Trait Implementations§
impl<T: ?Sized + Unpin> Unpin for Buffered<'_, T>
Auto Trait Implementations§
impl<'a, T> Freeze for Buffered<'a, T>where
T: ?Sized,
impl<'a, T> RefUnwindSafe for Buffered<'a, T>where
T: RefUnwindSafe + ?Sized,
impl<'a, T> !Send for Buffered<'a, T>
impl<'a, T> !Sync for Buffered<'a, T>
impl<'a, T> !UnwindSafe for Buffered<'a, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more