Struct scc::queue::Queue

source ·
pub struct Queue<T> { /* private fields */ }
Expand description

Queue is a lock-free concurrent first-in-first-out container.

Implementations§

source§

impl<T: 'static> Queue<T>

source

pub fn push(&self, val: T) -> Shared<Entry<T>>

Pushes an instance of T.

Returns a Shared holding a strong reference to the newly pushed entry.

§Examples
use scc::Queue;

let queue: Queue<usize> = Queue::default();

assert_eq!(**queue.push(11), 11);
source

pub fn push_if<F: FnMut(Option<&Entry<T>>) -> bool>( &self, val: T, cond: F ) -> Result<Shared<Entry<T>>, T>

Pushes an instance of T if the newest entry satisfies the given condition.

§Errors

Returns an error along with the supplied instance if the condition is not met.

§Examples
use scc::Queue;

let queue: Queue<usize> = Queue::default();

queue.push(11);

assert!(queue.push_if(17, |e| e.map_or(false, |x| **x == 11)).is_ok());
assert!(queue.push_if(29, |e| e.map_or(false, |x| **x == 11)).is_err());
source

pub fn peek<'g>(&self, guard: &'g Guard) -> Option<&'g Entry<T>>

Returns a guarded reference to the oldest entry.

Returns None if the Queue is empty. The returned reference can survive as long as the associated Guard is alive.

§Examples
use scc::ebr::Guard;
use scc::Queue;

let queue: Queue<usize> = Queue::default();

assert!(queue.peek(&Guard::new()).is_none());

queue.push(37);
queue.push(3);

assert_eq!(**queue.peek(&Guard::new()).unwrap(), 37);
source§

impl<T> Queue<T>

source

pub unsafe fn push_unchecked(&self, val: T) -> Shared<Entry<T>>

Pushes an instance of T without checking the lifetime of T.

Returns a Shared holding a strong reference to the newly pushed entry.

§Safety

T::drop can be run after the Queue is dropped, therefore it is safe only if T::drop does not access short-lived data or std::mem::needs_drop is false for T,

§Examples
use scc::Queue;

let hello = String::from("hello");
let queue: Queue<&str> = Queue::default();

assert_eq!(unsafe { **queue.push_unchecked(hello.as_str()) }, "hello");
source

pub unsafe fn push_if_unchecked<F: FnMut(Option<&Entry<T>>) -> bool>( &self, val: T, cond: F ) -> Result<Shared<Entry<T>>, T>

Pushes an instance of T if the newest entry satisfies the given condition without checking the lifetime of T

§Errors

Returns an error along with the supplied instance if the condition is not met.

§Safety

T::drop can be run after the Queue is dropped, therefore it is safe only if T::drop does not access short-lived data or std::mem::needs_drop is false for T,

§Examples
use scc::Queue;

let hello = String::from("hello");
let queue: Queue<&str> = Queue::default();

assert!(unsafe { queue.push_if_unchecked(hello.as_str(), |e| e.is_none()).is_ok() });
source

pub fn pop(&self) -> Option<Shared<Entry<T>>>

Pops the oldest entry.

Returns None if the Queue is empty.

§Examples
use scc::Queue;

let queue: Queue<usize> = Queue::default();

queue.push(37);
queue.push(3);
queue.push(1);

assert_eq!(queue.pop().map(|e| **e), Some(37));
assert_eq!(queue.pop().map(|e| **e), Some(3));
assert_eq!(queue.pop().map(|e| **e), Some(1));
assert!(queue.pop().is_none());
source

pub fn pop_if<F: FnMut(&Entry<T>) -> bool>( &self, cond: F ) -> Result<Option<Shared<Entry<T>>>, Shared<Entry<T>>>

Pops the oldest entry if the entry satisfies the given condition.

Returns None if the Queue is empty.

§Errors

Returns an error along with the oldest entry if the given condition is not met.

§Examples
use scc::Queue;

let queue: Queue<usize> = Queue::default();

queue.push(3);
queue.push(1);

assert!(queue.pop_if(|v| **v == 1).is_err());
assert_eq!(queue.pop().map(|e| **e), Some(3));
assert_eq!(queue.pop_if(|v| **v == 1).ok().and_then(|e| e).map(|e| **e), Some(1));
source

pub fn peek_with<R, F: FnOnce(Option<&Entry<T>>) -> R>(&self, reader: F) -> R

Peeks the oldest entry.

§Examples
use scc::Queue;

let queue: Queue<usize> = Queue::default();

assert!(queue.peek_with(|v| v.is_none()));

queue.push(37);
queue.push(3);

assert_eq!(queue.peek_with(|v| **v.unwrap()), 37);
source

pub fn len(&self) -> usize

Returns the number of entries in the Queue.

This method iterates over all the entries in the Queue to count them, therefore its time complexity is O(N).

§Examples
use scc::Queue;

let queue: Queue<usize> = Queue::default();
assert_eq!(queue.len(), 0);

queue.push(7);
queue.push(11);
assert_eq!(queue.len(), 2);

queue.pop();
queue.pop();
assert_eq!(queue.len(), 0);
source

pub fn is_empty(&self) -> bool

Returns true if the Queue is empty.

§Examples
use scc::Queue;

let queue: Queue<usize> = Queue::default();
assert!(queue.is_empty());

queue.push(7);
assert!(!queue.is_empty());
source

pub fn iter<'g>(&self, guard: &'g Guard) -> Iter<'g, T>

Returns an Iter.

§Examples
use scc::ebr::Guard;
use scc::Queue;

let queue: Queue<usize> = Queue::default();
assert_eq!(queue.iter(&Guard::new()).count(), 0);

queue.push(7);
queue.push(11);
queue.push(17);

let guard = Guard::new();
let mut iter = queue.iter(&guard);
assert_eq!(*iter.next().unwrap(), 7);
assert_eq!(*iter.next().unwrap(), 11);
assert_eq!(*iter.next().unwrap(), 17);
assert!(iter.next().is_none());

Trait Implementations§

source§

impl<T: Clone> Clone for Queue<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Queue<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T> Default for Queue<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Queue<T>

§

impl<T> RefUnwindSafe for Queue<T>

§

impl<T> Send for Queue<T>
where T: Send,

§

impl<T> Sync for Queue<T>
where T: Sync,

§

impl<T> Unpin for Queue<T>

§

impl<T> UnwindSafe for Queue<T>
where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.