跳到主要内容

SemaphorePermit

搜索

结构体 SemaphorePermit 

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

来自 semaphore 的 permit。

此类型由 acquire 方法创建。

实现§

Source§

impl<'a> SemaphorePermit<'a>

Source

pub fn forget(self)

放弃许可证而不会将其释放回信号量。这可用于减少信号量中可用的许可证数量。

§示例
use std::sync::Arc;


use tokio::sync::Semaphore;





let sem = Arc::new(Semaphore::new(10));


{


    let permit = sem.try_acquire_many(5).unwrap();


    assert_eq!(sem.available_permits(), 5);


    permit.forget();


}





// Since we forgot the permit, available permits won't go back to its initial value


// even after the permit is dropped.


assert_eq!(sem.available_permits(), 5);
Source

pub fn merge(&mut self, other: Self)

将两个 SemaphorePermit 实例合并在一起,消耗 other 而不释放其持有的许可证。

selfother 持有的许可证都会在 self 丢弃时释放。

§Panics

如果合并来自不同 Semaphore 实例的许可证,此函数会 panic。

§示例
use std::sync::Arc;


use tokio::sync::Semaphore;





let sem = Arc::new(Semaphore::new(10));


let mut permit = sem.try_acquire().unwrap();





for _ in 0..9 {


    let _permit = sem.try_acquire().unwrap();


    // Merge individual permits into a single one.


    permit.merge(_permit)


}





assert_eq!(sem.available_permits(), 0);





// Release all permits in a single batch.


drop(permit);





assert_eq!(sem.available_permits(), 10);
Source

pub fn split(&mut self, n: usize) -> Option<Self>

self 中拆分 n 个许可证,并返回持有 n 个许可证的新 SemaphorePermit 实例。

如果没有足够的许可证且无法减少 n,则返回 None

§示例
use std::sync::Arc;


use tokio::sync::Semaphore;





let sem = Arc::new(Semaphore::new(3));





let mut p1 = sem.try_acquire_many(3).unwrap();


let p2 = p1.split(1).unwrap();





assert_eq!(p1.num_permits(), 2);


assert_eq!(p2.num_permits(), 1);
Source

pub fn num_permits(&self) -> usize

返回 self 持有的许可证数量。

Trait 实现§

Source§

impl<'a> Debug for SemaphorePermit<'a>

Source§

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

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

impl Drop for SemaphorePermit<'_>

Source§

fn drop(&mut self)

执行此类型的析构函数。 更多信息

自动 Trait 实现§

§

impl<'a> Freeze for SemaphorePermit<'a>

§

impl<'a> !RefUnwindSafe for SemaphorePermit<'a>

§

impl<'a> Send for SemaphorePermit<'a>

§

impl<'a> Sync for SemaphorePermit<'a>

§

impl<'a> Unpin for SemaphorePermit<'a>

§

impl<'a> UnsafeUnpin for SemaphorePermit<'a>

§

impl<'a> !UnwindSafe for SemaphorePermit<'a>

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<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>

执行转换。