Runtime

Struct Runtime 

Source
pub struct Runtime { /* private fields */ }

Implementations§

Source§

impl Runtime

Source

pub fn new() -> Self

Create a new runtime instance

Examples found in repository?
examples/hello_world.rs (line 5)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async {
8        println!("Hello from Avila Async!");
9
10        sleep(Duration::from_secs(1)).await;
11
12        println!("One second later...");
13    });
14}
More examples
Hide additional examples
examples/timeout_demo.rs (line 15)
14fn main() {
15    let rt = Runtime::new();
16
17    rt.block_on(async {
18        // This will timeout
19        match timeout(Duration::from_secs(1), slow_operation()).await {
20            Ok(val) => println!("Slow operation completed: {}", val),
21            Err(_) => println!("Slow operation timed out!"),
22        }
23
24        // This will succeed
25        match timeout(Duration::from_secs(1), fast_operation()).await {
26            Ok(val) => println!("Fast operation completed: {}", val),
27            Err(_) => println!("Fast operation timed out!"),
28        }
29    });
30}
examples/parallel_tasks.rs (line 5)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async move {
8        println!("Spawning 100 concurrent tasks...");
9
10        let mut handles = vec![];
11
12        for i in 0..100 {
13            let handle = rt.spawn_with_handle(async move {
14                avila_async::sleep(Duration::from_millis(10)).await;
15                i * i
16            });
17            handles.push(handle);
18        }
19
20        println!("Waiting for all tasks to complete...");
21
22        let mut sum = 0;
23        for handle in handles {
24            if let Some(result) = handle.await_result().await {
25                sum += result;
26            }
27        }
28
29        println!("Sum of squares from 0 to 99: {}", sum);
30        println!("Active tasks: {}", rt.task_count());
31    });
32}
examples/channel_demo.rs (line 5)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async move {
8        let (tx, rx) = channel::bounded::<String>(10);
9
10        // Spawn producer task
11        rt.spawn({
12            let tx = tx.clone();
13            async move {
14                for i in 0..5 {
15                    let msg = format!("Message {}", i);
16                    println!("Sending: {}", msg);
17                    tx.send(msg).await.unwrap();
18                    avila_async::sleep(Duration::from_millis(500)).await;
19                }
20            }
21        });
22
23        // Spawn another producer
24        rt.spawn({
25            async move {
26                for i in 0..5 {
27                    let msg = format!("Urgent {}", i);
28                    println!("Sending: {}", msg);
29                    tx.send(msg).await.unwrap();
30                    avila_async::sleep(Duration::from_millis(300)).await;
31                }
32            }
33        });
34
35        // Receive messages
36        let mut count = 0;
37        while let Some(msg) = rx.recv().await {
38            println!("Received: {}", msg);
39            count += 1;
40            if count >= 10 {
41                break;
42            }
43        }
44
45        println!("All messages received!");
46    });
47}
Source

pub fn task_count(&self) -> usize

Get the number of active tasks

Examples found in repository?
examples/parallel_tasks.rs (line 30)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async move {
8        println!("Spawning 100 concurrent tasks...");
9
10        let mut handles = vec![];
11
12        for i in 0..100 {
13            let handle = rt.spawn_with_handle(async move {
14                avila_async::sleep(Duration::from_millis(10)).await;
15                i * i
16            });
17            handles.push(handle);
18        }
19
20        println!("Waiting for all tasks to complete...");
21
22        let mut sum = 0;
23        for handle in handles {
24            if let Some(result) = handle.await_result().await {
25                sum += result;
26            }
27        }
28
29        println!("Sum of squares from 0 to 99: {}", sum);
30        println!("Active tasks: {}", rt.task_count());
31    });
32}
Source

pub fn shutdown(&self)

Initiate graceful shutdown

Source

pub fn spawn<F>(&self, future: F)
where F: Future<Output = ()> + Send + 'static,

Spawn a future onto the runtime

Examples found in repository?
examples/channel_demo.rs (lines 11-21)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async move {
8        let (tx, rx) = channel::bounded::<String>(10);
9
10        // Spawn producer task
11        rt.spawn({
12            let tx = tx.clone();
13            async move {
14                for i in 0..5 {
15                    let msg = format!("Message {}", i);
16                    println!("Sending: {}", msg);
17                    tx.send(msg).await.unwrap();
18                    avila_async::sleep(Duration::from_millis(500)).await;
19                }
20            }
21        });
22
23        // Spawn another producer
24        rt.spawn({
25            async move {
26                for i in 0..5 {
27                    let msg = format!("Urgent {}", i);
28                    println!("Sending: {}", msg);
29                    tx.send(msg).await.unwrap();
30                    avila_async::sleep(Duration::from_millis(300)).await;
31                }
32            }
33        });
34
35        // Receive messages
36        let mut count = 0;
37        while let Some(msg) = rx.recv().await {
38            println!("Received: {}", msg);
39            count += 1;
40            if count >= 10 {
41                break;
42            }
43        }
44
45        println!("All messages received!");
46    });
47}
Source

pub fn spawn_with_handle<F, T>(&self, future: F) -> JoinHandle<T>
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Spawn a future and return a handle to await its result

Examples found in repository?
examples/parallel_tasks.rs (lines 13-16)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async move {
8        println!("Spawning 100 concurrent tasks...");
9
10        let mut handles = vec![];
11
12        for i in 0..100 {
13            let handle = rt.spawn_with_handle(async move {
14                avila_async::sleep(Duration::from_millis(10)).await;
15                i * i
16            });
17            handles.push(handle);
18        }
19
20        println!("Waiting for all tasks to complete...");
21
22        let mut sum = 0;
23        for handle in handles {
24            if let Some(result) = handle.await_result().await {
25                sum += result;
26            }
27        }
28
29        println!("Sum of squares from 0 to 99: {}", sum);
30        println!("Active tasks: {}", rt.task_count());
31    });
32}
Source

pub fn block_on<F, T>(&self, future: F) -> T
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Examples found in repository?
examples/hello_world.rs (lines 7-13)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async {
8        println!("Hello from Avila Async!");
9
10        sleep(Duration::from_secs(1)).await;
11
12        println!("One second later...");
13    });
14}
More examples
Hide additional examples
examples/timeout_demo.rs (lines 17-29)
14fn main() {
15    let rt = Runtime::new();
16
17    rt.block_on(async {
18        // This will timeout
19        match timeout(Duration::from_secs(1), slow_operation()).await {
20            Ok(val) => println!("Slow operation completed: {}", val),
21            Err(_) => println!("Slow operation timed out!"),
22        }
23
24        // This will succeed
25        match timeout(Duration::from_secs(1), fast_operation()).await {
26            Ok(val) => println!("Fast operation completed: {}", val),
27            Err(_) => println!("Fast operation timed out!"),
28        }
29    });
30}
examples/parallel_tasks.rs (lines 7-31)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async move {
8        println!("Spawning 100 concurrent tasks...");
9
10        let mut handles = vec![];
11
12        for i in 0..100 {
13            let handle = rt.spawn_with_handle(async move {
14                avila_async::sleep(Duration::from_millis(10)).await;
15                i * i
16            });
17            handles.push(handle);
18        }
19
20        println!("Waiting for all tasks to complete...");
21
22        let mut sum = 0;
23        for handle in handles {
24            if let Some(result) = handle.await_result().await {
25                sum += result;
26            }
27        }
28
29        println!("Sum of squares from 0 to 99: {}", sum);
30        println!("Active tasks: {}", rt.task_count());
31    });
32}
examples/channel_demo.rs (lines 7-46)
4fn main() {
5    let rt = Runtime::new();
6
7    rt.block_on(async move {
8        let (tx, rx) = channel::bounded::<String>(10);
9
10        // Spawn producer task
11        rt.spawn({
12            let tx = tx.clone();
13            async move {
14                for i in 0..5 {
15                    let msg = format!("Message {}", i);
16                    println!("Sending: {}", msg);
17                    tx.send(msg).await.unwrap();
18                    avila_async::sleep(Duration::from_millis(500)).await;
19                }
20            }
21        });
22
23        // Spawn another producer
24        rt.spawn({
25            async move {
26                for i in 0..5 {
27                    let msg = format!("Urgent {}", i);
28                    println!("Sending: {}", msg);
29                    tx.send(msg).await.unwrap();
30                    avila_async::sleep(Duration::from_millis(300)).await;
31                }
32            }
33        });
34
35        // Receive messages
36        let mut count = 0;
37        while let Some(msg) = rx.recv().await {
38            println!("Received: {}", msg);
39            count += 1;
40            if count >= 10 {
41                break;
42            }
43        }
44
45        println!("All messages received!");
46    });
47}

Trait Implementations§

Source§

impl Default for Runtime

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.