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

95
vendor/async-executor/examples/limit.rs vendored Normal file
View File

@@ -0,0 +1,95 @@
//! An executor where you can only push a limited number of tasks.
use async_executor::{Executor, Task};
use async_lock::Semaphore;
use std::{future::Future, sync::Arc, time::Duration};
/// An executor where you can only push a limited number of tasks.
struct LimitedExecutor {
/// Inner running executor.
executor: Executor<'static>,
/// Semaphore limiting the number of tasks.
semaphore: Arc<Semaphore>,
}
impl LimitedExecutor {
fn new(max: usize) -> Self {
Self {
executor: Executor::new(),
semaphore: Semaphore::new(max).into(),
}
}
/// Spawn a task, waiting until there is a slot available.
async fn spawn<F: Future + Send + 'static>(&self, future: F) -> Task<F::Output>
where
F::Output: Send + 'static,
{
// Wait for a semaphore permit.
let permit = self.semaphore.acquire_arc().await;
// Wrap it into a new future.
let future = async move {
let result = future.await;
drop(permit);
result
};
// Spawn the task.
self.executor.spawn(future)
}
/// Run a future to completion.
async fn run<F: Future>(&self, future: F) -> F::Output {
self.executor.run(future).await
}
}
fn main() {
futures_lite::future::block_on(async {
let ex = Arc::new(LimitedExecutor::new(10));
ex.run({
let ex = ex.clone();
async move {
// Spawn a bunch of tasks that wait for a while.
for i in 0..15 {
ex.spawn(async move {
async_io::Timer::after(Duration::from_millis(fastrand::u64(1..3))).await;
println!("Waiting task #{i} finished!");
})
.await
.detach();
}
let (start_tx, start_rx) = async_channel::bounded::<()>(1);
let mut current_rx = start_rx;
// Send the first message.
start_tx.send(()).await.unwrap();
// Spawn a bunch of channel tasks that wake eachother up.
for i in 0..25 {
let (next_tx, next_rx) = async_channel::bounded::<()>(1);
ex.spawn(async move {
current_rx.recv().await.unwrap();
println!("Channel task {i} woken up!");
next_tx.send(()).await.unwrap();
println!("Channel task {i} finished!");
})
.await
.detach();
current_rx = next_rx;
}
// Wait for the last task to finish.
current_rx.recv().await.unwrap();
println!("All tasks finished!");
}
})
.await;
});
}

View File

@@ -0,0 +1,84 @@
//! An executor with task priorities.
use std::thread;
use async_executor::{Executor, Task};
use futures_lite::{future, prelude::*};
/// Task priority.
#[repr(usize)]
#[derive(Debug, Clone, Copy)]
enum Priority {
High = 0,
Medium = 1,
Low = 2,
}
/// An executor with task priorities.
///
/// Tasks with lower priorities only get polled when there are no tasks with higher priorities.
struct PriorityExecutor<'a> {
ex: [Executor<'a>; 3],
}
impl<'a> PriorityExecutor<'a> {
/// Creates a new executor.
const fn new() -> PriorityExecutor<'a> {
PriorityExecutor {
ex: [Executor::new(), Executor::new(), Executor::new()],
}
}
/// Spawns a task with the given priority.
fn spawn<T: Send + 'a>(
&self,
priority: Priority,
future: impl Future<Output = T> + Send + 'a,
) -> Task<T> {
self.ex[priority as usize].spawn(future)
}
/// Runs the executor forever.
async fn run(&self) {
loop {
for _ in 0..200 {
let t0 = self.ex[0].tick();
let t1 = self.ex[1].tick();
let t2 = self.ex[2].tick();
// Wait until one of the ticks completes, trying them in order from highest
// priority to lowest priority.
t0.or(t1).or(t2).await;
}
// Yield every now and then.
future::yield_now().await;
}
}
}
fn main() {
static EX: PriorityExecutor<'_> = PriorityExecutor::new();
// Spawn a thread running the executor forever.
thread::spawn(|| future::block_on(EX.run()));
let mut tasks = Vec::new();
for _ in 0..20 {
// Choose a random priority.
let choice = [Priority::High, Priority::Medium, Priority::Low];
let priority = choice[fastrand::usize(..choice.len())];
// Spawn a task with this priority.
tasks.push(EX.spawn(priority, async move {
println!("{priority:?}");
future::yield_now().await;
println!("{priority:?}");
}));
}
for task in tasks {
future::block_on(task);
}
}