      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
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119

    #![cfg_attr(test, deny(warnings))]
    #![deny(missing_docs)]

    //! # variance
    //!
    //! Helpers for establishing the variance of lifetimes and type parameters.
    //!

    use std::marker::PhantomData;
    use std::fmt;
    use std::hash::{Hash, Hasher};
    use std::cmp::Ordering;

    /// A marker for forcing T to be considered invariant.
    // *mut T is invariant in T
    pub struct Invariant<T>(PhantomData<*mut T>);

    // TODO: We have to explicitly list all OIBITS here, which is not really possible.
    unsafe impl<T> Send for Invariant<T> {}
    unsafe impl<T> Sync for Invariant<T> {}

    impl<T> Invariant<T> {
        /// Create a new Invariant marker instance.
        ///
        /// All instances of Invariant with the same T are equivalent.
        #[inline]
        pub fn new() -> Self { Invariant(PhantomData) }
    }

    impl<T> Default for Invariant<T> {
        #[inline]
        fn default() -> Self { Invariant::new() }
    }

    impl<T> Copy for Invariant<T> {}

    impl<T> Clone for Invariant<T> {
        #[inline]
        fn clone(&self) -> Self { Invariant::new() }
    }

    impl<T> fmt::Debug for Invariant<T> {
        #[inline]
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.write_str("Invariant Type Marker")
        }
    }

    impl<T> PartialEq<Invariant<T>> for Invariant<T> {
        #[inline]
        fn eq(&self, _: &Self) -> bool { true }

        #[inline]
        fn ne(&self, _: &Self) -> bool { false }
    }

    impl<T> PartialOrd<Invariant<T>> for Invariant<T> {
        #[inline]
        fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
            Some(Ordering::Equal)
        }

        #[inline]
        fn lt(&self, _: &Self) -> bool { false }

        #[inline]
        fn le(&self, _: &Self) -> bool { true }

        #[inline]
        fn gt(&self, _: &Self) -> bool { false }

        #[inline]
        fn ge(&self, _: &Self) -> bool { true }
    }

    impl<T> Eq for Invariant<T> {}

    impl<T> Ord for Invariant<T> {
        #[inline]
        fn cmp(&self, _: &Self) -> Ordering { Ordering::Equal }
    }

    impl<T> Hash for Invariant<T> {
        #[inline]
        fn hash<H: Hasher>(&self, state: &mut H) {
            ().hash(state)
        }
    }

    /// A marker for forcing `'id` to be considered invariant.
    #[derive(Copy, Clone, Default, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
    pub struct InvariantLifetime<'id>(Invariant<&'id ()>);

    impl<'id> InvariantLifetime<'id> {
        /// Create a new InvariantLifetime marker instance.
        ///
        /// All instances of InvariantLifetime with the same lifetime are equivalent.
        #[inline]
        pub fn new() -> Self { InvariantLifetime::default() }
    }

    fn _assert_bounds() {
        fn is_send<T: Send>() {}
        fn is_sync<T: Sync>() {}
        fn is_derived<T: Copy + Clone + fmt::Debug + PartialEq + Eq + PartialOrd + Ord + Hash + Default>() {}

        struct Nothing;

        is_send::<Invariant<*mut ()>>();
        is_sync::<Invariant<*mut ()>>();
        is_derived::<Invariant<Nothing>>();

        fn lifetime<'a>() {
            is_send::<InvariantLifetime<'a>>();
            is_sync::<InvariantLifetime<'a>>();
            is_derived::<InvariantLifetime<'a>>();
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

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