跳到主要内容

Ref

搜索

结构体 Ref 

源代码
pub struct Ref<'a, T> { /* 私有字段 */ }
展开描述

返回对内部值的引用。

未释放的借用会对内部值保持一个读锁。这意味着长时间存活的借用可能会导致生产者一半被阻塞。建议尽量缩短借用的生命周期。此外,如果运行在允许 !Send future 的环境中,必须确保返回的 Ref 类型不会跨越 .await 点存活,否则可能会导致死锁。

锁的优先级策略取决于底层锁实现,本类型不保证使用任何特定策略。特别是,正在等待通过 send 获取锁的生产者可能会阻塞对 borrow 的并发调用,也可能不会,例如:

Potential deadlock example
// Task 1 (on thread A)    |  // Task 2 (on thread B)
let _ref1 = rx.borrow();   |
                           |  // will block
                           |  let _ = tx.send(());
// may deadlock            |
let _ref2 = rx.borrow();   |

实现§

源代码§

impl<'a, T> Ref<'a, T>

源代码

pub fn has_changed(&self) -> bool

指示自上次将借用值标记为已读取以来,该借用值是否被视为已更改

Receiver::has_changed() 不同,此方法在 channel 关闭时不会失败。

Sender 借用时,此函数将始终返回 false

§示例
use tokio::sync::watch;

let (tx, mut rx) = watch::channel("hello");

tx.send("goodbye").unwrap();
// The sender does never consider the value as changed.
assert!(!tx.borrow().has_changed());

// Drop the sender immediately, just for testing purposes.
drop(tx);

// Even if the sender has already been dropped...
assert!(rx.has_changed().is_err());
// ...the modified value is still readable and detected as changed.
assert_eq!(*rx.borrow(), "goodbye");
assert!(rx.borrow().has_changed());

// Read the changed value and mark it as seen.
{
    let received = rx.borrow_and_update();
    assert_eq!(*received, "goodbye");
    assert!(received.has_changed());
    // Release the read lock when leaving this scope.
}

// Now the value has already been marked as seen and could
// never be modified again (after the sender has been dropped).
assert!(!rx.borrow().has_changed());

trait 实现§

源代码§

impl<'a, T: Debug> Debug for Ref<'a, T>

源代码§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化器格式化此值。 更多信息
源代码§

impl<T> Deref for Ref<'_, T>

源代码§

type Target = T

解引用后得到的类型。
源代码§

fn deref(&self) -> &T

解引用此值。

自动 trait 实现§

§

impl<'a, T> Freeze for Ref<'a, T>

§

impl<'a, T> RefUnwindSafe for Ref<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> !Send for Ref<'a, T>

§

impl<'a, T> Sync for Ref<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Ref<'a, T>

§

impl<'a, T> UnsafeUnpin for Ref<'a, T>

§

impl<'a, T> UnwindSafe for Ref<'a, T>
where T: RefUnwindSafe,

blanket 实现§

源代码§

impl<T> Any for T
where T: 'static + ?Sized,

源代码§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
源代码§

impl<T> Borrow<T> for T
where T: ?Sized,

源代码§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
源代码§

impl<T> BorrowMut<T> for T
where T: ?Sized,

源代码§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 更多信息
源代码§

impl<T> From<T> for T

源代码§

fn from(t: T) -> T

原样返回参数。

源代码§

impl<T, U> Into<U> for T
where U: From<T>,

源代码§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换是 From<T> for U 实现选择执行的操作。

源代码§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

源代码§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
源代码§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

源代码§

type Error = Infallible

转换出错时返回的类型。
源代码§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
源代码§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

源代码§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
源代码§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。