spawn.rs.html -- source
Description: Source to the Rust file `libcore/task/spawn.rs`.
Keywords: rust, rustlang, rust-lang

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93

    // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
    // file at the top-level directory of this distribution and at
    // http://rust-lang.org/COPYRIGHT.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    #![unstable(feature = "futures_api",
                reason = "futures in libcore are unstable",
                issue = "50547")]

    use fmt;
    use future::{FutureObj, LocalFutureObj};

    /// Spawns tasks that poll futures to completion onto its associated task
    /// executor.
    ///
    /// The term "task" refers to a kind of lightweight "thread". Task executors
    /// are responsible for scheduling the execution of tasks on operating system
    /// threads.
    pub trait Spawn {
        /// Spawns a new task with the given future. The future will be polled until
        /// completion.
        ///
        /// # Errors
        ///
        /// The executor may be unable to spawn tasks, either because it has
        /// been shut down or is resource-constrained.
        fn spawn_obj(
            &mut self,
            future: FutureObj<'static, ()>,
        ) -> Result<(), SpawnObjError>;

        /// Determines whether the executor is able to spawn new tasks.
        ///
        /// # Returns
        ///
        /// An `Ok` return means the executor is *likely* (but not guaranteed)
        /// to accept a subsequent spawn attempt. Likewise, an `Err` return
        /// means that `spawn` is likely, but not guaranteed, to yield an error.
        #[inline]
        fn status(&self) -> Result<(), SpawnErrorKind> {
            Ok(())
        }
    }

    /// Provides the reason that an executor was unable to spawn.
    pub struct SpawnErrorKind {
        _hidden: (),
    }

    impl fmt::Debug for SpawnErrorKind {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_tuple("SpawnErrorKind")
                .field(&"shutdown")
                .finish()
        }
    }

    impl SpawnErrorKind {
        /// Spawning is failing because the executor has been shut down.
        pub fn shutdown() -> SpawnErrorKind {
            SpawnErrorKind { _hidden: () }
        }

        /// Check whether this error is the `shutdown` error.
        pub fn is_shutdown(&self) -> bool {
            true
        }
    }

    /// The result of a failed spawn
    #[derive(Debug)]
    pub struct SpawnObjError {
        /// The kind of error
        pub kind: SpawnErrorKind,

        /// The future for which spawning inside a task was attempted
        pub future: FutureObj<'static, ()>,
    }

    /// The result of a failed spawn
    #[derive(Debug)]
    pub struct SpawnLocalObjError {
        /// The kind of error
        pub kind: SpawnErrorKind,

        /// The future for which spawning inside a task was attempted
        pub future: LocalFutureObj<'static, ()>,
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)