Struct Queue

Source
pub struct Queue { /* private fields */ }
Expand description

A Grand Central Dispatch queue.

For more information, see Apple’s Grand Central Dispatch reference.

Implementations§

Source§

impl Queue

Source

pub fn main() -> Self

Returns the serial dispatch Queue associated with the application’s main thread.

Examples found in repository?
examples/main.rs (line 14)
12fn prompt(mut sum: i32, queue: Queue) {
13    queue.clone().exec_async(move || {
14        let main = Queue::main();
15        // Print our prompt on the main thread and wait until it's complete
16        main.exec_sync(|| {
17            println!("Enter a number:");
18        });
19
20        // Read the number the user enters
21        let mut input = String::new();
22        io::stdin().read_line(&mut input).unwrap();
23
24        if let Ok(num) = input.trim().parse::<i32>() {
25            sum += num;
26            // Print the sum on the main thread and wait until it's complete
27            main.exec_sync(|| {
28                println!("Sum is {}\n", sum);
29            });
30            // Do it again!
31            prompt(sum, queue);
32        } else {
33            // Bail if no number was entered
34            main.exec_async(|| {
35                println!("Not a number, exiting.");
36                exit(0);
37            });
38        }
39    });
40}
Source

pub fn global(priority: QueuePriority) -> Self

Returns a system-defined global concurrent Queue with the specified priority.

Examples found in repository?
examples/main.rs (line 46)
42fn main() {
43    // Read from stdin on a background queue so that the main queue is free
44    // to handle other events. All printing still occurs through the main
45    // queue to avoid jumbled output.
46    prompt(0, Queue::global(QueuePriority::Default));
47
48    unsafe {
49        dispatch::ffi::dispatch_main();
50    }
51}
Source

pub fn create(label: &str, attr: QueueAttribute) -> Self

Creates a new dispatch Queue.

Source

pub fn with_target_queue( label: &str, attr: QueueAttribute, target: &Queue, ) -> Self

Creates a new dispatch Queue with the given target queue.

A dispatch queue’s priority is inherited from its target queue. Additionally, if both the queue and its target are serial queues, their blocks will not be invoked concurrently.

Source

pub fn label(&self) -> &str

Returns the label that was specified for self.

Source

pub fn exec_sync<T, F>(&self, work: F) -> T
where F: Send + FnOnce() -> T, T: Send,

Submits a closure for execution on self and waits until it completes.

Examples found in repository?
examples/main.rs (lines 16-18)
12fn prompt(mut sum: i32, queue: Queue) {
13    queue.clone().exec_async(move || {
14        let main = Queue::main();
15        // Print our prompt on the main thread and wait until it's complete
16        main.exec_sync(|| {
17            println!("Enter a number:");
18        });
19
20        // Read the number the user enters
21        let mut input = String::new();
22        io::stdin().read_line(&mut input).unwrap();
23
24        if let Ok(num) = input.trim().parse::<i32>() {
25            sum += num;
26            // Print the sum on the main thread and wait until it's complete
27            main.exec_sync(|| {
28                println!("Sum is {}\n", sum);
29            });
30            // Do it again!
31            prompt(sum, queue);
32        } else {
33            // Bail if no number was entered
34            main.exec_async(|| {
35                println!("Not a number, exiting.");
36                exit(0);
37            });
38        }
39    });
40}
Source

pub fn exec_async<F>(&self, work: F)
where F: 'static + Send + FnOnce(),

Submits a closure for asynchronous execution on self and returns immediately.

Examples found in repository?
examples/main.rs (lines 13-39)
12fn prompt(mut sum: i32, queue: Queue) {
13    queue.clone().exec_async(move || {
14        let main = Queue::main();
15        // Print our prompt on the main thread and wait until it's complete
16        main.exec_sync(|| {
17            println!("Enter a number:");
18        });
19
20        // Read the number the user enters
21        let mut input = String::new();
22        io::stdin().read_line(&mut input).unwrap();
23
24        if let Ok(num) = input.trim().parse::<i32>() {
25            sum += num;
26            // Print the sum on the main thread and wait until it's complete
27            main.exec_sync(|| {
28                println!("Sum is {}\n", sum);
29            });
30            // Do it again!
31            prompt(sum, queue);
32        } else {
33            // Bail if no number was entered
34            main.exec_async(|| {
35                println!("Not a number, exiting.");
36                exit(0);
37            });
38        }
39    });
40}
Source

pub fn exec_after<F>(&self, delay: Duration, work: F)
where F: 'static + Send + FnOnce(),

After the specified delay, submits a closure for asynchronous execution on self.

Source

pub fn apply<F>(&self, iterations: usize, work: F)
where F: Sync + Fn(usize),

Submits a closure to be executed on self the given number of iterations and waits until it completes.

Source

pub fn for_each<T, F>(&self, slice: &mut [T], work: F)
where F: Sync + Fn(&mut T), T: Send,

Submits a closure to be executed on self for each element of the provided slice and waits until it completes.

Source

pub fn map<T, U, F>(&self, vec: Vec<T>, work: F) -> Vec<U>
where F: Sync + Fn(T) -> U, T: Send, U: Send,

Submits a closure to be executed on self for each element of the provided vector and returns a Vec of the mapped elements.

Source

pub fn barrier_sync<T, F>(&self, work: F) -> T
where F: Send + FnOnce() -> T, T: Send,

Submits a closure to be executed on self as a barrier and waits until it completes.

Barriers create synchronization points within a concurrent queue. If self is concurrent, when it encounters a barrier it delays execution of the closure (and any further ones) until all closures submitted before the barrier finish executing. At that point, the barrier closure executes by itself. Upon completion, self resumes its normal execution behavior.

If self is a serial queue or one of the global concurrent queues, this method behaves like the normal sync method.

Source

pub fn barrier_async<F>(&self, work: F)
where F: 'static + Send + FnOnce(),

Submits a closure to be executed on self as a barrier and returns immediately.

Barriers create synchronization points within a concurrent queue. If self is concurrent, when it encounters a barrier it delays execution of the closure (and any further ones) until all closures submitted before the barrier finish executing. At that point, the barrier closure executes by itself. Upon completion, self resumes its normal execution behavior.

If self is a serial queue or one of the global concurrent queues, this method behaves like the normal async method.

Source

pub fn suspend(&self) -> SuspendGuard

Suspends the invocation of blocks on self and returns a SuspendGuard that can be dropped to resume.

The suspension occurs after completion of any blocks running at the time of the call. Invocation does not resume until all SuspendGuards have been dropped.

Trait Implementations§

Source§

impl Clone for Queue

Source§

fn clone(&self) -> Self

Returns a duplicate 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 Debug for Queue

Source§

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

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

impl Drop for Queue

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Queue

Source§

impl Sync for Queue

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

Source§

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

Source§

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.