Skip to content

Instantly share code, notes, and snippets.

@zbentley
Last active March 4, 2024 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbentley/f45ea39843f533f5f54b23f8a49a5eae to your computer and use it in GitHub Desktop.
Save zbentley/f45ea39843f533f5f54b23f8a49a5eae to your computer and use it in GitHub Desktop.
Async Rust memory reuse
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