Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/runnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,23 @@ impl<M> Runnable<M> {
&self.header_with_metadata().metadata
}

/// Get a pointer to the metadata associated with this task.
///
/// Unlike [`Runnable::metadata`], this doesn't create any intermediate reference to
/// the task or the metadata, and doesn't require a reference to the task.
///
/// # Safety
///
/// Calling this method is always safe, but dereferencing the returned pointer isn't.
/// The pointer is only guaranteed to be valid for the scope between matching calls to
/// [`Runnable::into_raw`] and [`Runnable::from_raw`]. The returned pointer aliases the
/// references returned by [`Runnable::metadata`] and [`Task::metadata`].
pub fn metadata_raw(ptr: NonNull<()>) -> NonNull<M> {
let header: *mut HeaderWithMetadata<M> = ptr.cast().as_ptr();
// TODO: Once the MSRV reaches 1.82 this can use &raw mut instead
unsafe { NonNull::new_unchecked(core::ptr::addr_of_mut!((*header).metadata)) }
}

/// Schedules the task.
///
/// This is a convenience method that passes the [`Runnable`] to the schedule function.
Expand Down
45 changes: 44 additions & 1 deletion tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use async_task::{Builder, Runnable};
use flume::unbounded;
use smol::future;

use std::sync::atomic::{AtomicUsize, Ordering};
use std::{
ptr::NonNull,
sync::atomic::{AtomicUsize, Ordering},
};

#[test]
fn metadata_use_case() {
Expand Down Expand Up @@ -56,3 +59,43 @@ fn metadata_use_case() {
t2.await;
});
}

#[test]
fn metadata_raw() {
let (sender, receiver) = unbounded::<NonNull<()>>();

let future = |counter: &AtomicUsize| {
assert_eq!(0, counter.fetch_add(1, Ordering::SeqCst));
async {}
};

let schedule = move |runnable: Runnable<AtomicUsize>| {
let ptr = runnable.into_raw();

{
let counter: &AtomicUsize = unsafe { Runnable::metadata_raw(ptr).as_ref() };
assert_eq!(1, counter.fetch_add(1, Ordering::SeqCst));
}

sender.send(ptr).ok();
};

let (runnable, task) = unsafe {
Builder::new()
.metadata(AtomicUsize::new(0))
.spawn_unchecked(future, schedule)
};

runnable.schedule();
let ptr = receiver.recv().unwrap();

{
let counter: &AtomicUsize = unsafe { Runnable::metadata_raw(ptr).as_ref() };
assert_eq!(2, counter.fetch_add(1, Ordering::SeqCst));
}

let runnable: Runnable<AtomicUsize> = unsafe { Runnable::from_raw(ptr) };
assert_eq!(3, runnable.metadata().fetch_add(1, Ordering::SeqCst));

assert_eq!(4, task.metadata().load(Ordering::SeqCst));
}