Calling FnOnce inside a custom DST

⚓ Rust    📅 2025-09-13    👤 surdeus    👁️ 11      

surdeus

Warning

This post was published 61 days ago. The information described in this article may have changed.

Is there any way to call a FnOnce inside a custom DST in a box? Unsafe or unstable is okay.

struct TaskNode<T: FnOnce() + ?Sized> {
    next: Option<Box<TaskNode<dyn FnOnce()>>>,
    task: T,
}

fn run_tasks(mut head: Option<Box<TaskNode<dyn FnOnce()>>>) {
    while let Some(node) = head {
        head = node.next;
        // How to actually do this??
        (node.task)();
    }
}

8 posts - 3 participants

Read full topic

🏷️ Rust_feed