about summary refs log tree commit diff
path: root/src/libcore/ops
diff options
context:
space:
mode:
authorClar Charr <clar@charr.xyz>2017-06-07 22:16:16 -0400
committerClar Charr <clar@charr.xyz>2017-06-09 19:07:27 -0400
commitf8d5f90adee5e43ecf4cd0e191086f8bd45e1285 (patch)
tree1e9f98f55d7dc0b32a29f61268c4a1a7b51b249f /src/libcore/ops
parentb099e0e7867b7c2269c48f44892abf6e348ebda3 (diff)
downloadrust-f8d5f90adee5e43ecf4cd0e191086f8bd45e1285.tar.gz
rust-f8d5f90adee5e43ecf4cd0e191086f8bd45e1285.zip
Move Drop to module.
Diffstat (limited to 'src/libcore/ops')
-rw-r--r--src/libcore/ops/drop.rs99
-rw-r--r--src/libcore/ops/mod.rs94
2 files changed, 103 insertions, 90 deletions
diff --git a/src/libcore/ops/drop.rs b/src/libcore/ops/drop.rs
new file mode 100644
index 00000000000..92f3cb256c8
--- /dev/null
+++ b/src/libcore/ops/drop.rs
@@ -0,0 +1,99 @@
+// Copyright 2012 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.
+
+/// The `Drop` trait is used to run some code when a value goes out of scope.
+/// This is sometimes called a 'destructor'.
+///
+/// When a value goes out of scope, if it implements this trait, it will have
+/// its `drop` method called. Then any fields the value contains will also
+/// be dropped recursively.
+///
+/// Because of the recursive dropping, you do not need to implement this trait
+/// unless your type needs its own destructor logic.
+///
+/// # Examples
+///
+/// A trivial implementation of `Drop`. The `drop` method is called when `_x`
+/// goes out of scope, and therefore `main` prints `Dropping!`.
+///
+/// ```
+/// struct HasDrop;
+///
+/// impl Drop for HasDrop {
+///     fn drop(&mut self) {
+///         println!("Dropping!");
+///     }
+/// }
+///
+/// fn main() {
+///     let _x = HasDrop;
+/// }
+/// ```
+///
+/// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the
+/// `drop` method will be called first for `Outer`, then for `Inner`. Therefore
+/// `main` prints `Dropping Outer!` and then `Dropping Inner!`.
+///
+/// ```
+/// struct Inner;
+/// struct Outer(Inner);
+///
+/// impl Drop for Inner {
+///     fn drop(&mut self) {
+///         println!("Dropping Inner!");
+///     }
+/// }
+///
+/// impl Drop for Outer {
+///     fn drop(&mut self) {
+///         println!("Dropping Outer!");
+///     }
+/// }
+///
+/// fn main() {
+///     let _x = Outer(Inner);
+/// }
+/// ```
+///
+/// Because variables are dropped in the reverse order they are declared,
+/// `main` will print `Declared second!` and then `Declared first!`.
+///
+/// ```
+/// struct PrintOnDrop(&'static str);
+///
+/// fn main() {
+///     let _first = PrintOnDrop("Declared first!");
+///     let _second = PrintOnDrop("Declared second!");
+/// }
+/// ```
+#[lang = "drop"]
+#[stable(feature = "rust1", since = "1.0.0")]
+pub trait Drop {
+    /// A method called when the value goes out of scope.
+    ///
+    /// When this method has been called, `self` has not yet been deallocated.
+    /// If it were, `self` would be a dangling reference.
+    ///
+    /// After this function is over, the memory of `self` will be deallocated.
+    ///
+    /// This function cannot be called explicitly. This is compiler error
+    /// [E0040]. However, the [`std::mem::drop`] function in the prelude can be
+    /// used to call the argument's `Drop` implementation.
+    ///
+    /// [E0040]: ../../error-index.html#E0040
+    /// [`std::mem::drop`]: ../../std/mem/fn.drop.html
+    ///
+    /// # Panics
+    ///
+    /// Given that a `panic!` will call `drop()` as it unwinds, any `panic!` in
+    /// a `drop()` implementation will likely abort.
+    #[stable(feature = "rust1", since = "1.0.0")]
+    fn drop(&mut self);
+}
diff --git a/src/libcore/ops/mod.rs b/src/libcore/ops/mod.rs
index c725e9d0465..4e0389e5de4 100644
--- a/src/libcore/ops/mod.rs
+++ b/src/libcore/ops/mod.rs
@@ -150,6 +150,7 @@
 mod arith;
 mod bit;
 mod deref;
+mod drop;
 mod function;
 mod index;
 mod place;
@@ -171,6 +172,9 @@ pub use self::bit::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssig
 pub use self::deref::{Deref, DerefMut};
 
 #[stable(feature = "rust1", since = "1.0.0")]
+pub use self::drop::Drop;
+
+#[stable(feature = "rust1", since = "1.0.0")]
 pub use self::function::{Fn, FnMut, FnOnce};
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -193,93 +197,3 @@ pub use self::place::{Place, Placer, InPlace, Boxed, BoxPlace};
 
 #[unstable(feature = "coerce_unsized", issue = "27732")]
 pub use self::unsize::CoerceUnsized;
-
-/// The `Drop` trait is used to run some code when a value goes out of scope.
-/// This is sometimes called a 'destructor'.
-///
-/// When a value goes out of scope, if it implements this trait, it will have
-/// its `drop` method called. Then any fields the value contains will also
-/// be dropped recursively.
-///
-/// Because of the recursive dropping, you do not need to implement this trait
-/// unless your type needs its own destructor logic.
-///
-/// # Examples
-///
-/// A trivial implementation of `Drop`. The `drop` method is called when `_x`
-/// goes out of scope, and therefore `main` prints `Dropping!`.
-///
-/// ```
-/// struct HasDrop;
-///
-/// impl Drop for HasDrop {
-///     fn drop(&mut self) {
-///         println!("Dropping!");
-///     }
-/// }
-///
-/// fn main() {
-///     let _x = HasDrop;
-/// }
-/// ```
-///
-/// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the
-/// `drop` method will be called first for `Outer`, then for `Inner`. Therefore
-/// `main` prints `Dropping Outer!` and then `Dropping Inner!`.
-///
-/// ```
-/// struct Inner;
-/// struct Outer(Inner);
-///
-/// impl Drop for Inner {
-///     fn drop(&mut self) {
-///         println!("Dropping Inner!");
-///     }
-/// }
-///
-/// impl Drop for Outer {
-///     fn drop(&mut self) {
-///         println!("Dropping Outer!");
-///     }
-/// }
-///
-/// fn main() {
-///     let _x = Outer(Inner);
-/// }
-/// ```
-///
-/// Because variables are dropped in the reverse order they are declared,
-/// `main` will print `Declared second!` and then `Declared first!`.
-///
-/// ```
-/// struct PrintOnDrop(&'static str);
-///
-/// fn main() {
-///     let _first = PrintOnDrop("Declared first!");
-///     let _second = PrintOnDrop("Declared second!");
-/// }
-/// ```
-#[lang = "drop"]
-#[stable(feature = "rust1", since = "1.0.0")]
-pub trait Drop {
-    /// A method called when the value goes out of scope.
-    ///
-    /// When this method has been called, `self` has not yet been deallocated.
-    /// If it were, `self` would be a dangling reference.
-    ///
-    /// After this function is over, the memory of `self` will be deallocated.
-    ///
-    /// This function cannot be called explicitly. This is compiler error
-    /// [E0040]. However, the [`std::mem::drop`] function in the prelude can be
-    /// used to call the argument's `Drop` implementation.
-    ///
-    /// [E0040]: ../../error-index.html#E0040
-    /// [`std::mem::drop`]: ../../std/mem/fn.drop.html
-    ///
-    /// # Panics
-    ///
-    /// Given that a `panic!` will call `drop()` as it unwinds, any `panic!` in
-    /// a `drop()` implementation will likely abort.
-    #[stable(feature = "rust1", since = "1.0.0")]
-    fn drop(&mut self);
-}