Skip to content

Non-panicking variant of spawn_local()Β #695

@taufik-rama

Description

@taufik-rama

Hello πŸ‘‹

I have a question: from the spawn_local docs it is said that

pub fn spawn_local<T>(future: impl Future<Output = T> + 'static) -> Task<T> β“˜
where
    T: 'static,
---

Spawns a task onto the current single-threaded executor.

If called from a `LocalExecutor`, the task is spawned on it. Otherwise, this method panics.
...

And the implementation basically just delegates the call into a LOCAL_EX executor spawn() method

    pub fn spawn_local<T>(&self, future: impl Future<Output = T> + 'static) -> Task<T>
    where
        T: 'static,
    {
        #[cfg(not(feature = "native-tls"))]
        return LOCAL_EX.with(|local_ex| Task::<T>(local_ex.spawn(future)));

        #[cfg(feature = "native-tls")]
        return Task::<T>(unsafe {
            LOCAL_EX
                .as_ref()
                .expect("this thread doesn't have a LocalExecutor running")
                .spawn(future)
        });
    }

Would it be ok if that spawn() method on LocalExecutor struct just be made pub? So if the caller already have the LocalExecutor instance then they can just use it without any panic-risk (even though it would essentially never happens)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions