Skip to content

Commit bfa80f7

Browse files
committed
auto merge of #10344 : brson/rust/atomiccopy, r=alexcrichton
I didn't try to add clone methods since it's not clear to me which types are appropriate to clone or what the memory ordering should be.
2 parents 57d1ed8 + eabdc8c commit bfa80f7

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

src/libstd/unstable/atomics.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,46 @@ use cast;
2323
use option::{Option,Some,None};
2424
use libc::c_void;
2525
use ops::Drop;
26+
use util::NonCopyable;
2627

2728
/**
2829
* A simple atomic flag, that can be set and cleared. The most basic atomic type.
2930
*/
3031
pub struct AtomicFlag {
31-
priv v: int
32+
priv v: int,
33+
priv nocopy: NonCopyable
3234
}
3335

3436
/**
3537
* An atomic boolean type.
3638
*/
3739
pub struct AtomicBool {
38-
priv v: uint
40+
priv v: uint,
41+
priv nocopy: NonCopyable
3942
}
4043

4144
/**
4245
* A signed atomic integer type, supporting basic atomic arithmetic operations
4346
*/
4447
pub struct AtomicInt {
45-
priv v: int
48+
priv v: int,
49+
priv nocopy: NonCopyable
4650
}
4751

4852
/**
4953
* An unsigned atomic integer type, supporting basic atomic arithmetic operations
5054
*/
5155
pub struct AtomicUint {
52-
priv v: uint
56+
priv v: uint,
57+
priv nocopy: NonCopyable
5358
}
5459

5560
/**
5661
* An unsafe atomic pointer. Only supports basic atomic operations
5762
*/
5863
pub struct AtomicPtr<T> {
59-
priv p: *mut T
64+
priv p: *mut T,
65+
priv nocopy: NonCopyable
6066
}
6167

6268
/**
@@ -75,15 +81,15 @@ pub enum Ordering {
7581
SeqCst
7682
}
7783

78-
pub static INIT_ATOMIC_FLAG : AtomicFlag = AtomicFlag { v: 0 };
79-
pub static INIT_ATOMIC_BOOL : AtomicBool = AtomicBool { v: 0 };
80-
pub static INIT_ATOMIC_INT : AtomicInt = AtomicInt { v: 0 };
81-
pub static INIT_ATOMIC_UINT : AtomicUint = AtomicUint { v: 0 };
84+
pub static INIT_ATOMIC_FLAG : AtomicFlag = AtomicFlag { v: 0, nocopy: NonCopyable };
85+
pub static INIT_ATOMIC_BOOL : AtomicBool = AtomicBool { v: 0, nocopy: NonCopyable };
86+
pub static INIT_ATOMIC_INT : AtomicInt = AtomicInt { v: 0, nocopy: NonCopyable };
87+
pub static INIT_ATOMIC_UINT : AtomicUint = AtomicUint { v: 0, nocopy: NonCopyable };
8288

8389
impl AtomicFlag {
8490

8591
pub fn new() -> AtomicFlag {
86-
AtomicFlag { v: 0 }
92+
AtomicFlag { v: 0, nocopy: NonCopyable }
8793
}
8894

8995
/**
@@ -106,7 +112,7 @@ impl AtomicFlag {
106112

107113
impl AtomicBool {
108114
pub fn new(v: bool) -> AtomicBool {
109-
AtomicBool { v: if v { 1 } else { 0 } }
115+
AtomicBool { v: if v { 1 } else { 0 }, nocopy: NonCopyable }
110116
}
111117

112118
#[inline]
@@ -171,7 +177,7 @@ impl AtomicBool {
171177

172178
impl AtomicInt {
173179
pub fn new(v: int) -> AtomicInt {
174-
AtomicInt { v:v }
180+
AtomicInt { v:v, nocopy: NonCopyable }
175181
}
176182

177183
#[inline]
@@ -209,7 +215,7 @@ impl AtomicInt {
209215

210216
impl AtomicUint {
211217
pub fn new(v: uint) -> AtomicUint {
212-
AtomicUint { v:v }
218+
AtomicUint { v:v, nocopy: NonCopyable }
213219
}
214220

215221
#[inline]
@@ -247,7 +253,7 @@ impl AtomicUint {
247253

248254
impl<T> AtomicPtr<T> {
249255
pub fn new(p: *mut T) -> AtomicPtr<T> {
250-
AtomicPtr { p:p }
256+
AtomicPtr { p:p, nocopy: NonCopyable }
251257
}
252258

253259
#[inline]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Issue #8380
12+
13+
#[feature(globs)];
14+
15+
use std::unstable::atomics::*;
16+
use std::ptr;
17+
18+
fn main() {
19+
let x = INIT_ATOMIC_FLAG;
20+
let x = *&x; //~ ERROR: cannot move out of dereference
21+
let x = INIT_ATOMIC_BOOL;
22+
let x = *&x; //~ ERROR: cannot move out of dereference
23+
let x = INIT_ATOMIC_INT;
24+
let x = *&x; //~ ERROR: cannot move out of dereference
25+
let x = INIT_ATOMIC_UINT;
26+
let x = *&x; //~ ERROR: cannot move out of dereference
27+
let x: AtomicPtr<uint> = AtomicPtr::new(ptr::mut_null());
28+
let x = *&x; //~ ERROR: cannot move out of dereference
29+
let x: AtomicOption<uint> = AtomicOption::empty();
30+
let x = *&x; //~ ERROR: cannot move out of dereference
31+
}

0 commit comments

Comments
 (0)