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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use discard;
use discard::Discard;
use std::ops::{Deref, DerefMut};


/// If you have a value which implements [`Discard`](https://docs.rs/discard/%5E1.0.3/discard/trait.Discard.html), you can use
/// [`DiscardOnDrop::new(value)`](struct.DiscardOnDrop.html#method.new) which will wrap the value.
/// When the wrapper is dropped it will automatically call [`value.discard()`](https://docs.rs/discard/%5E1.0.3/discard/trait.Discard.html#tymethod.discard).
///
/// You can use the [`leak`](#method.leak) method to unwrap it (which returns `value`). This causes
/// it to no longer call [`discard`](https://docs.rs/discard/%5E1.0.3/discard/trait.Discard.html#tymethod.discard) when it is dropped, which
/// means it will usually leak memory unless you manually call [`discard`](https://docs.rs/discard/%5E1.0.3/discard/trait.Discard.html#tymethod.discard).
#[must_use = "

     The DiscardOnDrop is unused, which causes it to be immediately discarded.
     You probably don't want that to happen.

     How to fix this:

       * Store the DiscardOnDrop in a variable or data structure.

       * Or use the leak() method which will cause it to not be
         discarded (this will usually leak memory!)

     See the documentation for more details.
"]
#[derive(Debug)]
pub struct DiscardOnDrop< A: Discard >( discard::DiscardOnDrop< A > );

impl< A: Discard > DiscardOnDrop< A > {
    /// Creates a new `DiscardOnDrop`.
    ///
    /// When the `DiscardOnDrop` is dropped it will automatically call [`discarder.discard()`](https://docs.rs/discard/%5E1.0.3/discard/trait.Discard.html#tymethod.discard).
    #[inline]
    pub fn new( discarder: A ) -> Self {
        DiscardOnDrop( discard::DiscardOnDrop::new( discarder ) )
    }

    /// Returns the wrapped `discarder`.
    ///
    /// It will no longer automatically call [`discarder.discard()`](https://docs.rs/discard/%5E1.0.3/discard/trait.Discard.html#tymethod.discard), so this will usually leak
    /// memory unless you manually call [`discarder.discard()`](https://docs.rs/discard/%5E1.0.3/discard/trait.Discard.html#tymethod.discard).
    #[inline]
    pub fn leak( self ) -> A {
        discard::DiscardOnDrop::leak( self.0 )
    }
}

impl< A: Discard > Deref for DiscardOnDrop< A > {
    type Target = A;

    #[inline]
    fn deref( &self ) -> &Self::Target {
        &*self.0
    }
}

impl< A: Discard > DerefMut for DiscardOnDrop< A > {
    #[inline]
    fn deref_mut( &mut self ) -> &mut Self::Target {
        &mut *self.0
    }
}


#[cfg(test)]
mod tests {
    use discard::Discard;
    use super::DiscardOnDrop;
    use std::rc::Rc;
    use std::cell::Cell;

    struct Foo( Rc< Cell< bool > > );

    impl Foo {
        fn new() -> Self {
            Foo( Rc::new( Cell::new( false ) ) )
        }

        fn dropped( &self ) -> Rc< Cell< bool > > {
            self.0.clone()
        }

        fn as_mut( &mut self ) -> &mut Self {
            self
        }
    }

    impl Discard for Foo {
        fn discard( self ) {
            self.0.set( true );
        }
    }


    #[test]
    fn unused() {
        Foo::new();
    }

    #[test]
    #[allow(unused_must_use)]
    fn unused_discard_on_drop() {
        DiscardOnDrop::new( Foo::new() );
    }

    #[test]
    fn discard() {
        let foo = Foo::new();

        let dropped = foo.dropped();

        assert_eq!( dropped.get(), false );
        foo.discard();
        assert_eq!( dropped.get(), true );
    }

    #[test]
    fn no_discard() {
        let foo = Foo::new();

        let dropped = foo.dropped();

        assert_eq!( dropped.get(), false );
        drop( foo );
        assert_eq!( dropped.get(), false );
    }

    #[test]
    fn discard_on_drop() {
        let foo = DiscardOnDrop::new( Foo::new() );

        let dropped = foo.dropped();

        assert_eq!( dropped.get(), false );
        drop( foo );
        assert_eq!( dropped.get(), true );
    }

    #[test]
    fn leak() {
        let foo = DiscardOnDrop::new(Foo::new());

        let dropped = foo.dropped();

        assert_eq!( dropped.get(), false );
        drop( foo.leak() );
        assert_eq!( dropped.get(), false );
    }

    #[test]
    fn deref_mut() {
        let mut foo = DiscardOnDrop::new( Foo::new() );

        let dropped = foo.as_mut().dropped();

        assert_eq!( dropped.get(), false );
        drop( foo.leak() );
        assert_eq!( dropped.get(), false );
    }
}