@@ -75,18 +75,14 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
75
75
}
76
76
77
77
/// A non-copyable dummy type.
78
+ #[ deriving( Eq , TotalEq , Ord , TotalOrd ) ]
79
+ #[ no_drop_flag]
78
80
pub struct NonCopyable ;
79
81
80
- impl NonCopyable {
81
- /// Creates a dummy non-copyable structure and returns it for use.
82
- pub fn new ( ) -> NonCopyable { NonCopyable }
83
- }
84
-
85
82
impl Drop for NonCopyable {
86
83
fn drop ( & self ) { }
87
84
}
88
85
89
-
90
86
/// A type with no inhabitants
91
87
pub enum Void { }
92
88
@@ -130,39 +126,70 @@ pub fn unreachable() -> ! {
130
126
131
127
#[ cfg( test) ]
132
128
mod tests {
129
+ use super :: * ;
133
130
use option:: { None , Some } ;
134
- use util:: { Void , NonCopyable , id, replace, swap} ;
135
131
use either:: { Either , Left , Right } ;
132
+ use sys:: size_of;
133
+ use kinds:: Drop ;
136
134
137
135
#[ test]
138
- pub fn identity_crisis ( ) {
136
+ fn identity_crisis ( ) {
139
137
// Writing a test for the identity function. How did it come to this?
140
138
let x = ~[ ( 5 , false ) ] ;
141
139
//FIXME #3387 assert!(x.eq(id(copy x)));
142
140
let y = copy x;
143
141
assert ! ( x. eq( & id( y) ) ) ;
144
142
}
143
+
145
144
#[ test]
146
- pub fn test_swap ( ) {
145
+ fn test_swap ( ) {
147
146
let mut x = 31337 ;
148
147
let mut y = 42 ;
149
148
swap ( & mut x, & mut y) ;
150
149
assert_eq ! ( x, 42 ) ;
151
150
assert_eq ! ( y, 31337 ) ;
152
151
}
152
+
153
153
#[ test]
154
- pub fn test_replace ( ) {
155
- let mut x = Some ( NonCopyable :: new ( ) ) ;
154
+ fn test_replace ( ) {
155
+ let mut x = Some ( NonCopyable ) ;
156
156
let y = replace ( & mut x, None ) ;
157
157
assert ! ( x. is_none( ) ) ;
158
158
assert ! ( y. is_some( ) ) ;
159
159
}
160
+
160
161
#[ test]
161
- pub fn test_uninhabited ( ) {
162
+ fn test_uninhabited ( ) {
162
163
let could_only_be_coin : Either < Void , ( ) > = Right ( ( ) ) ;
163
164
match could_only_be_coin {
164
165
Right ( coin) => coin,
165
166
Left ( is_void) => is_void. uninhabited ( )
166
167
}
167
168
}
169
+
170
+ #[ test]
171
+ fn test_noncopyable ( ) {
172
+ assert_eq ! ( size_of:: <NonCopyable >( ) , 0 ) ;
173
+
174
+ // verify that `#[no_drop_flag]` works as intended on a zero-size struct
175
+
176
+ static mut did_run: bool = false ;
177
+
178
+ struct Foo { five : int }
179
+
180
+ impl Drop for Foo {
181
+ fn drop ( & self ) {
182
+ assert_eq ! ( self . five, 5 ) ;
183
+ unsafe {
184
+ did_run = true ;
185
+ }
186
+ }
187
+ }
188
+
189
+ {
190
+ let _a = ( NonCopyable , Foo { five : 5 } , NonCopyable ) ;
191
+ }
192
+
193
+ unsafe { assert_eq ! ( did_run, true ) ; }
194
+ }
168
195
}
0 commit comments