diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-06-27 03:32:21 -0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-06-27 23:20:42 -0400 |
| commit | 779ee2a2dd06235470cf5b6bea90b78619dc0654 (patch) | |
| tree | 8cb9c6c672fb0a5d376349d1e8dd37f695c29860 /src/libstd | |
| parent | 927f454ac115c7d19f34951914b269ce356a1ca6 (diff) | |
| download | rust-779ee2a2dd06235470cf5b6bea90b78619dc0654.tar.gz rust-779ee2a2dd06235470cf5b6bea90b78619dc0654.zip | |
util: make NonCopyable 0 size (instead of 1 byte)
this also adds a derived Eq, TotalEq, Ord and TotalOrd along with removing the useless constructor
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/option.rs | 2 | ||||
| -rw-r--r-- | src/libstd/util.rs | 51 |
2 files changed, 40 insertions, 13 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs index f3ea81f1ae5..12ae2abd4a1 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -447,7 +447,7 @@ fn test_option_dance() { } #[test] #[should_fail] #[ignore(cfg(windows))] fn test_option_too_much_dance() { - let mut y = Some(util::NonCopyable::new()); + let mut y = Some(util::NonCopyable); let _y2 = y.swap_unwrap(); let _y3 = y.swap_unwrap(); } diff --git a/src/libstd/util.rs b/src/libstd/util.rs index 6eddae17ce6..e66271df478 100644 --- a/src/libstd/util.rs +++ b/src/libstd/util.rs @@ -75,18 +75,14 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T { } /// A non-copyable dummy type. +#[deriving(Eq, TotalEq, Ord, TotalOrd)] +#[no_drop_flag] pub struct NonCopyable; -impl NonCopyable { - /// Creates a dummy non-copyable structure and returns it for use. - pub fn new() -> NonCopyable { NonCopyable } -} - impl Drop for NonCopyable { fn drop(&self) { } } - /// A type with no inhabitants pub enum Void { } @@ -130,39 +126,70 @@ pub fn unreachable() -> ! { #[cfg(test)] mod tests { + use super::*; use option::{None, Some}; - use util::{Void, NonCopyable, id, replace, swap}; use either::{Either, Left, Right}; + use sys::size_of; + use kinds::Drop; #[test] - pub fn identity_crisis() { + fn identity_crisis() { // Writing a test for the identity function. How did it come to this? let x = ~[(5, false)]; //FIXME #3387 assert!(x.eq(id(copy x))); let y = copy x; assert!(x.eq(&id(y))); } + #[test] - pub fn test_swap() { + fn test_swap() { let mut x = 31337; let mut y = 42; swap(&mut x, &mut y); assert_eq!(x, 42); assert_eq!(y, 31337); } + #[test] - pub fn test_replace() { - let mut x = Some(NonCopyable::new()); + fn test_replace() { + let mut x = Some(NonCopyable); let y = replace(&mut x, None); assert!(x.is_none()); assert!(y.is_some()); } + #[test] - pub fn test_uninhabited() { + fn test_uninhabited() { let could_only_be_coin : Either <Void, ()> = Right (()); match could_only_be_coin { Right (coin) => coin, Left (is_void) => is_void.uninhabited () } } + + #[test] + fn test_noncopyable() { + assert_eq!(size_of::<NonCopyable>(), 0); + + // verify that `#[no_drop_flag]` works as intended on a zero-size struct + + static mut did_run: bool = false; + + struct Foo { five: int } + + impl Drop for Foo { + fn drop(&self) { + assert_eq!(self.five, 5); + unsafe { + did_run = true; + } + } + } + + { + let _a = (NonCopyable, Foo { five: 5 }, NonCopyable); + } + + unsafe { assert_eq!(did_run, true); } + } } |
