2023-10-19 21:46:28 +00:00
|
|
|
#![feature(coroutines, coroutine_trait)]
|
2020-01-27 22:26:37 +00:00
|
|
|
|
2023-10-19 16:06:43 +00:00
|
|
|
use std::ops::{Coroutine, CoroutineState};
|
2020-01-27 22:26:37 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
struct SetToNone<'a: 'b, 'b>(&'b mut Option<&'a i32>);
|
|
|
|
|
|
|
|
impl<'a, 'b> Drop for SetToNone<'a, 'b> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
*self.0 = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
fn drop_using_coroutine() -> i32 {
|
2020-01-27 22:26:37 +00:00
|
|
|
let mut y = Some(&0);
|
|
|
|
let z = &mut y;
|
|
|
|
let r;
|
|
|
|
{
|
|
|
|
let mut g = move |r| {
|
|
|
|
let _s = SetToNone(r);
|
|
|
|
yield;
|
|
|
|
};
|
|
|
|
let mut g = Pin::new(&mut g);
|
|
|
|
g.as_mut().resume(z);
|
|
|
|
r = y.as_ref().unwrap();
|
|
|
|
//~^ ERROR cannot borrow `y` as immutable because it is also borrowed as mutable
|
|
|
|
}
|
|
|
|
**r
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2023-10-19 21:46:28 +00:00
|
|
|
println!("{}", drop_using_coroutine());
|
2020-01-27 22:26:37 +00:00
|
|
|
}
|