context.rs.html -- source
Description: Source to the Rust file `libcore/task/context.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
    94
    95
    96
    97
    98

    // 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 super::{Spawn, Waker, LocalWaker};

    /// Information about the currently-running task.
    ///
    /// Contexts are always tied to the stack, since they are set up specifically
    /// when performing a single `poll` step on a task.
    pub struct Context<'a> {
        local_waker: &'a LocalWaker,
        spawner: &'a mut dyn Spawn,
    }

    impl<'a> fmt::Debug for Context<'a> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_struct("Context")
                .finish()
        }
    }

    impl<'a> Context<'a> {
        /// Create a new task `Context` with the provided `local_waker`, `waker`,
        /// and `spawner`.
        #[inline]
        pub fn new(
            local_waker: &'a LocalWaker,
            spawner: &'a mut dyn Spawn,
        ) -> Context<'a> {
            Context { local_waker, spawner }
        }

        /// Get the `LocalWaker` associated with the current task.
        #[inline]
        pub fn local_waker(&self) -> &'a LocalWaker {
            self.local_waker
        }

        /// Get the `Waker` associated with the current task.
        #[inline]
        pub fn waker(&self) -> &'a Waker {
            unsafe { &*(self.local_waker as *const LocalWaker as *const Waker) }
        }

        /// Get the spawner associated with this task.
        ///
        /// This method is useful primarily if you want to explicitly handle
        /// spawn failures.
        #[inline]
        pub fn spawner(&mut self) -> &mut dyn Spawn {
            self.spawner
        }

        /// Produce a context like the current one, but using the given waker
        /// instead.
        ///
        /// This advanced method is primarily used when building "internal
        /// schedulers" within a task, where you want to provide some customized
        /// wakeup logic.
        #[inline]
        pub fn with_waker<'b>(
            &'b mut self,
            local_waker: &'b LocalWaker,
        ) -> Context<'b> {
            Context {
                local_waker,
                spawner: self.spawner,
            }
        }

        /// Produce a context like the current one, but using the given spawner
        /// instead.
        ///
        /// This advanced method is primarily used when building "internal
        /// schedulers" within a task.
        #[inline]
        pub fn with_spawner<'b, Sp: Spawn>(
            &'b mut self,
            spawner: &'b mut Sp,
        ) -> Context<'b> {
            Context {
                local_waker: self.local_waker,
                spawner,
            }
        }
    }

# 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`)