# try_lock::TryLock - Rust
API documentation for the Rust `TryLock` struct in crate `try_lock`.
Keywords: rust, rustlang, rust-lang, TryLock

# Struct try_lock::TryLock [−] [src]

    pub struct TryLock<T> { /* fields omitted */ }

A light-weight lock guarded by an atomic boolean.

Most efficient when contention is low, acquiring the lock is a single atomic swap, and releasing it just 1 more atomic swap.

It is only possible to try to acquire the lock, it is not possible to wait for the lock to become ready, like with a `Mutex`.

## Implementations

### `impl<T> TryLock<T>` [src]

#### `pub fn new(val: T) -> TryLock<T>` [src]

Create a `TryLock` around the value.

#### `pub fn try_lock(&self) -> Option<Locked<'_, T>>` [src]

Try to acquire the lock of this value.

If the lock is already acquired by someone else, this returns `None`. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.

# Note

The default memory ordering is to use `Acquire` to lock, and `Release` to unlock. If different ordering is required, use `try_lock_explicit` or `try_lock_explicit_unchecked`.

#### `pub fn try_lock_order(
&self,
lock_order: Ordering,
unlock_order: Ordering
) -> Option<Locked<'_, T>>` [src]

👎 Deprecated since 0.2.3:

This method is actually unsafe because it unsafely allows the use of weaker memory ordering. Please use try_lock_explicit instead

Try to acquire the lock of this value using the lock and unlock orderings.

If the lock is already acquired by someone else, this returns `None`. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.

#### `pub fn try_lock_explicit(
&self,
lock_order: Ordering,
unlock_order: Ordering
) -> Option<Locked<'_, T>>` [src]

Try to acquire the lock of this value using the specified lock and unlock orderings.

If the lock is already acquired by someone else, this returns `None`. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.

# Panic

This method panics if `lock_order` is not any of `Acquire`, `AcqRel`, and `SeqCst`, or `unlock_order` is not any of `Release` and `SeqCst`.

#### `pub unsafe fn try_lock_explicit_unchecked(
&self,
lock_order: Ordering,
unlock_order: Ordering
) -> Option<Locked<'_, T>>` [src]

Try to acquire the lock of this value using the specified lock and unlock orderings without checking that the specified orderings are strong enough to be safe.

If the lock is already acquired by someone else, this returns `None`. You can try to acquire again whenever you want, perhaps by spinning a few times, or by using some other means of notification.

# Safety

Unlike `try_lock_explicit`, this method is unsafe because it does not check that the given memory orderings are strong enough to prevent data race.

#### `pub fn into_inner(self) -> T` [src]

Take the value back out of the lock when this is the sole owner.

## Trait Implementations

### `impl<T: Debug> Debug for TryLock<T>` [src]

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter. Read more

### `impl<T: Default> Default for TryLock<T>` [src]

#### `fn default() -> TryLock<T>` [src]

Returns the "default value" for a type. Read more

### `impl<T: Send> Send for TryLock<T>` [src]

### `impl<T: Send> Sync for TryLock<T>` [src]

## Auto Trait Implementations

### `impl<T> Unpin for TryLock<T> where
T: Unpin,` [src]

## Blanket Implementations

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `pub fn type_id(&self) -> TypeId` [src]

Gets the `TypeId` of `self`. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow(&self) -> &T` [src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `pub fn borrow_mut(&mut self) -> &mut T` [src]

Mutably borrows from an owned value. Read more

### `impl<T> From<T> for T` [src]

#### `pub fn from(t: T) -> T` [src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `pub fn into(self) -> U` [src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>` [src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

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

The type returned in the event of a conversion error.

#### `pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>` [src]

Performs the conversion.