#stable #hack #resolved #state #borrow #cell #borrow-stable

已撤销 stable-borrow-state

在#27733问题解决前,使用borrow_stable方法的hack

使用旧的Rust 2015

0.1.0 2016年7月18日

#5 in #resolved

3KB
56 代码行

stable-borrow-state

在#27733问题解决前,hack使用稳定Rust中的borrow_stable方法。

extern crate stable_borrow_state;

use stable_borrow_state::{BorrowState, borrow_state};
use std::cell::RefCell;

struct Dummy {
    a: Vec<bool>
}

let a = RefCell::new(Dummy {
    a: vec![true, false, false, false]
});
assert_eq!(borrow_state(&a), BorrowState::Unused);

{
    let mut b = a.borrow_mut();
    b.a.push(true);
    assert_eq!(borrow_state(&a), BorrowState::Writing);
}

assert_eq!(borrow_state(&a), BorrowState::Unused);

{
    let b = a.borrow();
    assert_eq!(borrow_state(&a), BorrowState::Reading);
    let c = a.borrow();
    assert_eq!(borrow_state(&a), BorrowState::Reading);

    drop(b);
    drop(c);
}

assert_eq!(borrow_state(&a), BorrowState::Unused);

许可证

MIT或Apache-2.0,任选其一

无运行时依赖