Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
//! A simple single-threaded executor that can spawn non-`Send` futures.
use std::cell::Cell;
use std::future::Future;
use std::rc::Rc;
use async_task::{Runnable, Task};
thread_local! {
// A queue that holds scheduled tasks.
static QUEUE: (flume::Sender<Runnable>, flume::Receiver<Runnable>) = flume::unbounded();
}
/// Spawns a future on the executor.
fn spawn<F, T>(future: F) -> Task<T>
where
F: Future<Output = T> + 'static,
T: 'static,
{
// Create a task that is scheduled by pushing itself into the queue.
let schedule = |runnable| QUEUE.with(|(s, _)| s.send(runnable).unwrap());
let (runnable, task) = async_task::spawn_local(future, schedule);
// Schedule the task by pushing it into the queue.
runnable.schedule();
task
}
/// Runs a future to completion.
fn run<F, T>(future: F) -> T
where
F: Future<Output = T> + 'static,
T: 'static,
{
// Spawn a task that sends its result through a channel.
let (s, r) = flume::unbounded();
spawn(async move { drop(s.send(future.await)) }).detach();
loop {
// If the original task has completed, return its result.
if let Ok(val) = r.try_recv() {
return val;
}
// Otherwise, take a task from the queue and run it.
QUEUE.with(|(_, r)| r.recv().unwrap().run());
}
}
fn main() {
let val = Rc::new(Cell::new(0));
// Run a future that increments a non-`Send` value.
run({
let val = val.clone();
async move {
// Spawn a future that increments the value.
let task = spawn({
let val = val.clone();
async move {
val.set(dbg!(val.get()) + 1);
}
});
val.set(dbg!(val.get()) + 1);
task.await;
}
});
// The value should be 2 at the end of the program.
dbg!(val.get());
}

View File

@@ -0,0 +1,53 @@
//! A function that runs a future to completion on a dedicated thread.
use std::future::Future;
use std::sync::Arc;
use std::thread;
use async_task::Task;
use smol::future;
/// Spawns a future on a new dedicated thread.
///
/// The returned task can be used to await the output of the future.
fn spawn_on_thread<F, T>(future: F) -> Task<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
// Create a channel that holds the task when it is scheduled for running.
let (sender, receiver) = flume::unbounded();
let sender = Arc::new(sender);
let s = Arc::downgrade(&sender);
// Wrap the future into one that disconnects the channel on completion.
let future = async move {
// When the inner future completes, the sender gets dropped and disconnects the channel.
let _sender = sender;
future.await
};
// Create a task that is scheduled by sending it into the channel.
let schedule = move |runnable| s.upgrade().unwrap().send(runnable).unwrap();
let (runnable, task) = async_task::spawn(future, schedule);
// Schedule the task by sending it into the channel.
runnable.schedule();
// Spawn a thread running the task to completion.
thread::spawn(move || {
// Keep taking the task from the channel and running it until completion.
for runnable in receiver {
runnable.run();
}
});
task
}
fn main() {
// Spawn a future on a dedicated thread.
future::block_on(spawn_on_thread(async {
println!("Hello, world!");
}));
}

48
vendor/async-task/examples/spawn.rs vendored Normal file
View File

@@ -0,0 +1,48 @@
//! A simple single-threaded executor.
use std::future::Future;
use std::panic::catch_unwind;
use std::thread;
use async_task::{Runnable, Task};
use once_cell::sync::Lazy;
use smol::future;
/// Spawns a future on the executor.
fn spawn<F, T>(future: F) -> Task<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
// A queue that holds scheduled tasks.
static QUEUE: Lazy<flume::Sender<Runnable>> = Lazy::new(|| {
let (sender, receiver) = flume::unbounded::<Runnable>();
// Start the executor thread.
thread::spawn(|| {
for runnable in receiver {
// Ignore panics inside futures.
let _ignore_panic = catch_unwind(|| runnable.run());
}
});
sender
});
// Create a task that is scheduled by pushing it into the queue.
let schedule = |runnable| QUEUE.send(runnable).unwrap();
let (runnable, task) = async_task::spawn(future, schedule);
// Schedule the task by pushing it into the queue.
runnable.schedule();
task
}
fn main() {
// Spawn a future and await its result.
let task = spawn(async {
println!("Hello, world!");
});
future::block_on(task);
}

View File

@@ -0,0 +1,145 @@
//! A single threaded executor that uses shortest-job-first scheduling.
use std::cell::RefCell;
use std::collections::BinaryHeap;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::thread;
use std::time::{Duration, Instant};
use std::{cell::Cell, future::Future};
use async_task::{Builder, Runnable, Task};
use pin_project_lite::pin_project;
use smol::{channel, future};
struct ByDuration(Runnable<DurationMetadata>);
impl ByDuration {
fn duration(&self) -> Duration {
self.0.metadata().inner.get()
}
}
impl PartialEq for ByDuration {
fn eq(&self, other: &Self) -> bool {
self.duration() == other.duration()
}
}
impl Eq for ByDuration {}
impl PartialOrd for ByDuration {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ByDuration {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.duration().cmp(&other.duration()).reverse()
}
}
pin_project! {
#[must_use = "futures do nothing unless you `.await` or poll them"]
struct MeasureRuntime<'a, F> {
#[pin]
f: F,
duration: &'a Cell<Duration>
}
}
impl<'a, F: Future> Future for MeasureRuntime<'a, F> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let duration_cell: &Cell<Duration> = this.duration;
let start = Instant::now();
let res = F::poll(this.f, cx);
let new_duration = Instant::now() - start;
duration_cell.set(duration_cell.get() / 2 + new_duration / 2);
res
}
}
pub struct DurationMetadata {
inner: Cell<Duration>,
}
thread_local! {
// A queue that holds scheduled tasks.
static QUEUE: RefCell<BinaryHeap<ByDuration>> = RefCell::new(BinaryHeap::new());
}
fn make_future_fn<'a, F>(
future: F,
) -> impl (FnOnce(&'a DurationMetadata) -> MeasureRuntime<'a, F>) {
move |duration_meta| MeasureRuntime {
f: future,
duration: &duration_meta.inner,
}
}
fn ensure_safe_schedule<F: Send + Sync + 'static>(f: F) -> F {
f
}
/// Spawns a future on the executor.
pub fn spawn<F, T>(future: F) -> Task<T, DurationMetadata>
where
F: Future<Output = T> + 'static,
T: 'static,
{
let spawn_thread_id = thread::current().id();
// Create a task that is scheduled by pushing it into the queue.
let schedule = ensure_safe_schedule(move |runnable| {
if thread::current().id() != spawn_thread_id {
panic!("Task would be run on a different thread than spawned on.");
}
QUEUE.with(move |queue| queue.borrow_mut().push(ByDuration(runnable)));
});
let future_fn = make_future_fn(future);
let (runnable, task) = unsafe {
Builder::new()
.metadata(DurationMetadata {
inner: Cell::new(Duration::default()),
})
.spawn_unchecked(future_fn, schedule)
};
// Schedule the task by pushing it into the queue.
runnable.schedule();
task
}
pub fn block_on<F>(future: F)
where
F: Future<Output = ()> + 'static,
{
let task = spawn(future);
while !task.is_finished() {
let Some(runnable) = QUEUE.with(|queue| queue.borrow_mut().pop()) else {
thread::yield_now();
continue;
};
runnable.0.run();
}
}
fn main() {
// Spawn a future and await its result.
block_on(async {
let (sender, receiver) = channel::bounded(1);
let world = spawn(async move {
receiver.recv().await.unwrap();
println!("world.")
});
let hello = spawn(async move {
sender.send(()).await.unwrap();
print!("Hello, ")
});
future::zip(hello, world).await;
});
}