Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created October 15, 2018 04:47
Show Gist options
  • Select an option

  • Save rust-play/4460c62e6963112a7fcb258de08abf9a to your computer and use it in GitHub Desktop.

Select an option

Save rust-play/4460c62e6963112a7fcb258de08abf9a to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::cell::RefCell;
use std::collections::BinaryHeap;
use std::collections::binary_heap::PeekMut;
#[derive(Ord, PartialOrd, Eq, PartialEq)]
struct Task(u32);
struct Foo {
heap: RefCell<BinaryHeap<Task>>,
}
impl Foo {
pub fn run_for(self: &Foo) {
loop {
let mut heap = self.heap.borrow_mut();
let entry = if let Some(entry) = heap.peek_mut() {
entry
} else {
break;
};
if entry.0 < 50 {
break;
}
let task = PeekMut::pop(entry);
// Try uncommenting these:
//drop(entry);
//drop(heap);
// scheduled_tasks MUST be unborrowed before calling this
self.run_single_task(task);
}
}
fn run_single_task(self: &Foo, _task: Task) {
self.heap.borrow_mut();
println!("We made it");
}
}
fn main() {
let mut heap = BinaryHeap::new();
heap.push(Task(32));
heap.push(Task(100));
let foo = Foo { heap: heap.into() };
foo.run_for();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment