Last active
March 4, 2024 16:32
-
-
Save zbentley/f45ea39843f533f5f54b23f8a49a5eae to your computer and use it in GitHub Desktop.
Async Rust memory reuse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use tokio::select; | |
use tokio::time::{sleep, Duration}; | |
struct CanDrop { | |
a: Vec<i32>, | |
} | |
impl Drop for CanDrop { | |
fn drop(&mut self) { | |
println!("Dropping!") | |
} | |
} | |
async fn memory_hungry() { | |
let mut x = Box::new(CanDrop { | |
a: vec![0; 90_PERCENT_OF_SYSTEM_RAM], | |
}); | |
println!("memory_hungry() starting sleep"); | |
sleep(Duration::from_secs(2)).await; | |
println!("memory_hungry() finished sleep"); | |
x.a[0] = 1; // Use the data so as to avoid any optimizations that elide it | |
} | |
#[tokio::main] | |
async fn main() { | |
println!("Starting select"); | |
select! { | |
_ = memory_hungry() => unreachable!(), | |
_ = sleep(Duration::from_secs(1)) => println!("slept 1"), | |
} | |
println!("Done with select"); | |
memory_hungry().await; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment