diff options
| author | bors <bors@rust-lang.org> | 2014-10-10 00:07:08 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-10-10 00:07:08 +0000 |
| commit | f9fc49c06e5c71a8d63d6120e1a92b6445fb501d (patch) | |
| tree | 8a7c52db87b4ff8d5a7fa97dc44fb9ac10b92626 /src/libsync | |
| parent | 8b12fb326b50c40c7b5acadb4403c7021fedb272 (diff) | |
| parent | 0b517117b3ac9c4981bbe00a529e48e4019554d1 (diff) | |
| download | rust-f9fc49c06e5c71a8d63d6120e1a92b6445fb501d.tar.gz rust-f9fc49c06e5c71a8d63d6120e1a92b6445fb501d.zip | |
auto merge of #17853 : alexcrichton/rust/issue-17718, r=pcwalton
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
Closes #17718
[rfc]: https://github.com/rust-lang/rfcs/pull/246
Diffstat (limited to 'src/libsync')
| -rw-r--r-- | src/libsync/comm/oneshot.rs | 6 | ||||
| -rw-r--r-- | src/libsync/comm/shared.rs | 8 | ||||
| -rw-r--r-- | src/libsync/comm/stream.rs | 6 | ||||
| -rw-r--r-- | src/libsync/mutex.rs | 8 | ||||
| -rw-r--r-- | src/libsync/one.rs | 2 |
5 files changed, 15 insertions, 15 deletions
diff --git a/src/libsync/comm/oneshot.rs b/src/libsync/comm/oneshot.rs index 5b3cf33ebf0..053b5dc4c8a 100644 --- a/src/libsync/comm/oneshot.rs +++ b/src/libsync/comm/oneshot.rs @@ -43,9 +43,9 @@ use atomic; use comm::Receiver; // Various states you can find a port in. -static EMPTY: uint = 0; -static DATA: uint = 1; -static DISCONNECTED: uint = 2; +const EMPTY: uint = 0; +const DATA: uint = 1; +const DISCONNECTED: uint = 2; pub struct Packet<T> { // Internal state of the chan/port pair (stores the blocked task as well) diff --git a/src/libsync/comm/shared.rs b/src/libsync/comm/shared.rs index cb35bd8afb7..cfd045d0882 100644 --- a/src/libsync/comm/shared.rs +++ b/src/libsync/comm/shared.rs @@ -31,12 +31,12 @@ use rustrt::thread::Thread; use atomic; use mpsc_queue as mpsc; -static DISCONNECTED: int = int::MIN; -static FUDGE: int = 1024; +const DISCONNECTED: int = int::MIN; +const FUDGE: int = 1024; #[cfg(test)] -static MAX_STEALS: int = 5; +const MAX_STEALS: int = 5; #[cfg(not(test))] -static MAX_STEALS: int = 1 << 20; +const MAX_STEALS: int = 1 << 20; pub struct Packet<T> { queue: mpsc::Queue<T>, diff --git a/src/libsync/comm/stream.rs b/src/libsync/comm/stream.rs index 36fe335128e..8e433c6a585 100644 --- a/src/libsync/comm/stream.rs +++ b/src/libsync/comm/stream.rs @@ -30,11 +30,11 @@ use atomic; use comm::Receiver; use spsc_queue as spsc; -static DISCONNECTED: int = int::MIN; +const DISCONNECTED: int = int::MIN; #[cfg(test)] -static MAX_STEALS: int = 5; +const MAX_STEALS: int = 5; #[cfg(not(test))] -static MAX_STEALS: int = 1 << 20; +const MAX_STEALS: int = 1 << 20; pub struct Packet<T> { queue: spsc::Queue<Message<T>>, // internal queue for all message diff --git a/src/libsync/mutex.rs b/src/libsync/mutex.rs index 12de615a81b..c6413d0d09c 100644 --- a/src/libsync/mutex.rs +++ b/src/libsync/mutex.rs @@ -70,9 +70,9 @@ use rustrt::thread::Thread; use mpsc_intrusive as q; -pub static LOCKED: uint = 1 << 0; -pub static GREEN_BLOCKED: uint = 1 << 1; -pub static NATIVE_BLOCKED: uint = 1 << 2; +pub const LOCKED: uint = 1 << 0; +pub const GREEN_BLOCKED: uint = 1 << 1; +pub const NATIVE_BLOCKED: uint = 1 << 2; /// A mutual exclusion primitive useful for protecting shared data /// @@ -163,7 +163,7 @@ pub struct Guard<'a> { /// Static initialization of a mutex. This constant can be used to initialize /// other mutex constants. -pub static MUTEX_INIT: StaticMutex = StaticMutex { +pub const MUTEX_INIT: StaticMutex = StaticMutex { lock: mutex::NATIVE_MUTEX_INIT, state: atomic::INIT_ATOMIC_UINT, flavor: UnsafeCell { value: Unlocked }, diff --git a/src/libsync/one.rs b/src/libsync/one.rs index 4594345d2a3..c740c4f3d2e 100644 --- a/src/libsync/one.rs +++ b/src/libsync/one.rs @@ -45,7 +45,7 @@ pub struct Once { } /// Initialization value for static `Once` values. -pub static ONCE_INIT: Once = Once { +pub const ONCE_INIT: Once = Once { mutex: MUTEX_INIT, cnt: atomic::INIT_ATOMIC_INT, lock_cnt: atomic::INIT_ATOMIC_INT, |
