mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-06 00:00:10 +02:00
runtime: update Handle::current to mention EnterGuard (#4567)
Handle::current docs say it's not possible to call it on any non-runtime thread, but you can call it from a runtime context created by an EnterGuard. This updates the docs to mention EnterGuard as a way to avoid this panic.
This commit is contained in:
@@ -63,7 +63,8 @@ pub struct EnterGuard<'a> {
|
|||||||
impl Handle {
|
impl Handle {
|
||||||
/// Enters the runtime context. This allows you to construct types that must
|
/// Enters the runtime context. This allows you to construct types that must
|
||||||
/// have an executor available on creation such as [`Sleep`] or [`TcpStream`].
|
/// have an executor available on creation such as [`Sleep`] or [`TcpStream`].
|
||||||
/// It will also allow you to call methods such as [`tokio::spawn`].
|
/// It will also allow you to call methods such as [`tokio::spawn`] and [`Handle::current`]
|
||||||
|
/// without panicking.
|
||||||
///
|
///
|
||||||
/// [`Sleep`]: struct@crate::time::Sleep
|
/// [`Sleep`]: struct@crate::time::Sleep
|
||||||
/// [`TcpStream`]: struct@crate::net::TcpStream
|
/// [`TcpStream`]: struct@crate::net::TcpStream
|
||||||
@@ -80,8 +81,9 @@ impl Handle {
|
|||||||
/// # Panic
|
/// # Panic
|
||||||
///
|
///
|
||||||
/// This will panic if called outside the context of a Tokio runtime. That means that you must
|
/// This will panic if called outside the context of a Tokio runtime. That means that you must
|
||||||
/// call this on one of the threads **being run by the runtime**. Calling this from within a
|
/// call this on one of the threads **being run by the runtime**, or from a thread with an active
|
||||||
/// thread created by `std::thread::spawn` (for example) will cause a panic.
|
/// `EnterGuard`. Calling this from within a thread created by `std::thread::spawn` (for example)
|
||||||
|
/// will cause a panic unless that thread has an active `EnterGuard`.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
@@ -105,9 +107,14 @@ impl Handle {
|
|||||||
/// # let handle =
|
/// # let handle =
|
||||||
/// thread::spawn(move || {
|
/// thread::spawn(move || {
|
||||||
/// // Notice that the handle is created outside of this thread and then moved in
|
/// // Notice that the handle is created outside of this thread and then moved in
|
||||||
/// handle.spawn(async { /* ... */ })
|
/// handle.spawn(async { /* ... */ });
|
||||||
/// // This next line would cause a panic
|
/// // This next line would cause a panic because we haven't entered the runtime
|
||||||
/// // let handle2 = Handle::current();
|
/// // and created an EnterGuard
|
||||||
|
/// // let handle2 = Handle::current(); // panic
|
||||||
|
/// // So we create a guard here with Handle::enter();
|
||||||
|
/// let _guard = handle.enter();
|
||||||
|
/// // Now we can call Handle::current();
|
||||||
|
/// let handle2 = Handle::current();
|
||||||
/// });
|
/// });
|
||||||
/// # handle.join().unwrap();
|
/// # handle.join().unwrap();
|
||||||
/// # });
|
/// # });
|
||||||
|
|||||||
Reference in New Issue
Block a user