pub struct Runtime { /* private fields */ }Implementations§
Source§impl Runtime
impl Runtime
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new runtime instance
Examples found in repository?
More 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}Sourcepub fn task_count(&self) -> usize
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}Sourcepub fn spawn<F>(&self, future: F)
pub fn spawn<F>(&self, future: F)
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}Sourcepub fn spawn_with_handle<F, T>(&self, future: F) -> JoinHandle<T>
pub fn spawn_with_handle<F, T>(&self, future: F) -> JoinHandle<T>
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}Sourcepub fn block_on<F, T>(&self, future: F) -> T
pub fn block_on<F, T>(&self, future: F) -> T
Examples found in repository?
More 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§
Auto Trait Implementations§
impl Freeze for Runtime
impl RefUnwindSafe for Runtime
impl Send for Runtime
impl Sync for Runtime
impl Unpin for Runtime
impl UnwindSafe for Runtime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more