Skip to main content

std/sync/mpmc/
select.rs

1/// Temporary data that gets initialized during a blocking operation, and is consumed by
2/// `read` or `write`.
3///
4/// Each field contains data associated with a specific channel flavor.
5#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Token {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "Token",
            "array", &self.array, "list", &self.list, "zero", &&self.zero)
    }
}Debug, #[automatically_derived]
impl ::core::default::Default for Token {
    #[inline]
    fn default() -> Token {
        Token {
            array: ::core::default::Default::default(),
            list: ::core::default::Default::default(),
            zero: ::core::default::Default::default(),
        }
    }
}Default)]
6pub struct Token {
7    pub(crate) array: super::array::ArrayToken,
8    pub(crate) list: super::list::ListToken,
9    #[allow(dead_code)]
10    pub(crate) zero: super::zero::ZeroToken,
11}
12
13/// Identifier associated with an operation by a specific thread on a specific channel.
14#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Operation {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Operation",
            &&self.0)
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for Operation {
    #[inline]
    fn clone(&self) -> Operation {
        let _: ::core::clone::AssertParamIsClone<usize>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Operation { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for Operation {
    #[inline]
    fn eq(&self, other: &Operation) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Operation {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<usize>;
    }
}Eq)]
15pub struct Operation(usize);
16
17impl Operation {
18    /// Creates an operation identifier from a mutable reference.
19    ///
20    /// This function essentially just turns the address of the reference into a number. The
21    /// reference should point to a variable that is specific to the thread and the operation,
22    /// and is alive for the entire duration of a blocking operation.
23    #[inline]
24    pub fn hook<T>(r: &mut T) -> Operation {
25        let val = r as *mut T as usize;
26        // Make sure that the pointer address doesn't equal the numerical representation of
27        // `Selected::{Waiting, Aborted, Disconnected}`.
28        if !(val > 2) { ::core::panicking::panic("assertion failed: val > 2") };assert!(val > 2);
29        Operation(val)
30    }
31}
32
33/// Current state of a blocking operation.
34#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Selected {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Selected::Waiting =>
                ::core::fmt::Formatter::write_str(f, "Waiting"),
            Selected::Aborted =>
                ::core::fmt::Formatter::write_str(f, "Aborted"),
            Selected::Disconnected =>
                ::core::fmt::Formatter::write_str(f, "Disconnected"),
            Selected::Operation(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Operation", &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for Selected {
    #[inline]
    fn clone(&self) -> Selected {
        let _: ::core::clone::AssertParamIsClone<Operation>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Selected { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for Selected {
    #[inline]
    fn eq(&self, other: &Selected) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (Selected::Operation(__self_0), Selected::Operation(__arg1_0))
                    => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Selected {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Operation>;
    }
}Eq)]
35pub enum Selected {
36    /// Still waiting for an operation.
37    Waiting,
38
39    /// The attempt to block the current thread has been aborted.
40    Aborted,
41
42    /// An operation became ready because a channel is disconnected.
43    Disconnected,
44
45    /// An operation became ready because a message can be sent or received.
46    Operation(Operation),
47}
48
49impl From<usize> for Selected {
50    #[inline]
51    fn from(val: usize) -> Selected {
52        match val {
53            0 => Selected::Waiting,
54            1 => Selected::Aborted,
55            2 => Selected::Disconnected,
56            oper => Selected::Operation(Operation(oper)),
57        }
58    }
59}
60
61impl Into<usize> for Selected {
62    #[inline]
63    fn into(self) -> usize {
64        match self {
65            Selected::Waiting => 0,
66            Selected::Aborted => 1,
67            Selected::Disconnected => 2,
68            Selected::Operation(Operation(val)) => val,
69        }
70    }
71}