跳到主要内容

Ref

搜索

结构体 Ref 

Source
pub struct Ref<'a, T> { /* private fields */ }
展开描述

返回内部值的引用。

未完成的借用 会对内部值 持有读锁。 这意味着 长时间存活的借用 可能会 导致生产者一半阻塞。 建议 尽可能 保持借用 短暂存在。 此外, 如果你在 允许 !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();   |

实现§

Source§

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

Source

pub fn has_changed(&self) -> bool

指示借用的值自上次被标记为已读以来是否被视为已更改。

与 Receiver::has_changed() 不同,如果通道已关闭,此方法不会失败。

从 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 实现§

Source§

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

Source§

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

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

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

Source§

type Target = T

解引用后得到的类型。
Source§

fn deref(&self) -> &T

解引用此值。

自动 Trait 实现§

§

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

§

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

§

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>

Blanket 实现§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. 更多信息
Source§

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

Source§

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

Mutably borrows from an owned value. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

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

Source§

fn into(self) -> U

调用 U::from(self)

也就是说,此转换的具体行为取决于 From<T> for U 的实现方式。

Source§

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

Source§

type Target = T

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

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

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

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

执行转换。
Source§

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

Source§

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

转换出错时返回的类型。
Source§

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

执行转换。