You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These APIs were removed prior to async-task 4.0 (in commit deb709f); they can however be useful to e.g. pass Runnable as a context argument to GCD's dispatch_async_f, which takes an opaque *mut () as an argument. Without exposing from_raw/into_raw one has to box the Runnable in order to get something that can be passed across that boundary, which seems wasteful.
/// To avoid a memory leak the pointer must be converted back to a Runnable using [`Runnable<M>::from_raw`][from_raw].
824
+
///
825
+
/// `into_raw` does not change the state of the [`Task`], but there is no guarantee that it will be in the same state after calling [`Runnable<M>::from_raw`][from_raw],
826
+
/// as the corresponding [`Task`] might have been dropped or cancelled.
827
+
///
828
+
/// # Examples
829
+
///
830
+
/// ```rust
831
+
/// use async_task::{Runnable, spawn};
832
+
833
+
/// let (runnable, task) = spawn(async {}, |_| {});
834
+
/// let runnable_pointer = runnable.into_raw();
835
+
///
836
+
/// unsafe {
837
+
/// // Convert back to an `Runnable` to prevent leak.
838
+
/// let runnable = Runnable::<()>::from_raw(runnable_pointer);
839
+
/// runnable.run();
840
+
/// // Further calls to `Runnable::from_raw(runnable_pointer)` would be memory-unsafe.
841
+
/// }
842
+
/// // The memory was freed when `x` went out of scope above, so `runnable_pointer` is now dangling!
843
+
/// ```
844
+
/// [from_raw]: #method.from_raw
845
+
pubfninto_raw(self) -> NonNull<()>{
846
+
let ptr = self.ptr;
847
+
mem::forget(self);
848
+
ptr
849
+
}
850
+
851
+
/// Converts a raw pointer into a Runnable.
852
+
///
853
+
/// # Safety
854
+
///
855
+
/// This method should only be used with raw pointers returned from [`Runnable<M>::into_raw`][into_raw].
856
+
/// It is not safe to use the provided pointer once it is passed to `from_raw`.
857
+
/// Crucially, it is unsafe to call `from_raw` multiple times with the same pointer - even if the resulting [`Runnable`] is not used -
858
+
/// as internally `async-task` uses reference counting.
859
+
///
860
+
/// It is however safe to call [`Runnable<M>::into_raw`][into_raw] on a [`Runnable`] created with `from_raw` or
861
+
/// after the [`Task`] associated with a given Runnable has been dropped or cancelled.
862
+
///
863
+
/// The state of the [`Runnable`] created with `from_raw` is not specified.
864
+
///
865
+
/// # Examples
866
+
///
867
+
/// ```rust
868
+
/// use async_task::{Runnable, spawn};
869
+
870
+
/// let (runnable, task) = spawn(async {}, |_| {});
871
+
/// let runnable_pointer = runnable.into_raw();
872
+
///
873
+
/// drop(task);
874
+
/// unsafe {
875
+
/// // Convert back to an `Runnable` to prevent leak.
876
+
/// let runnable = Runnable::<()>::from_raw(runnable_pointer);
877
+
/// let did_poll = runnable.run();
878
+
/// assert!(!did_poll);
879
+
/// // Further calls to `Runnable::from_raw(runnable_pointer)` would be memory-unsafe.
880
+
/// }
881
+
/// // The memory was freed when `x` went out of scope above, so `runnable_pointer` is now dangling!
0 commit comments