Skip to content
This repository was archived by the owner on Aug 12, 2021. It is now read-only.

Commit 98acfa0

Browse files
committed
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]: rust-lang/rfcs#246
2 parents c4919db + ae3b90d commit 98acfa0

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

lib.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,23 @@ pub mod color {
155155
/// Number for a terminal color
156156
pub type Color = u16;
157157

158-
pub static BLACK: Color = 0u16;
159-
pub static RED: Color = 1u16;
160-
pub static GREEN: Color = 2u16;
161-
pub static YELLOW: Color = 3u16;
162-
pub static BLUE: Color = 4u16;
163-
pub static MAGENTA: Color = 5u16;
164-
pub static CYAN: Color = 6u16;
165-
pub static WHITE: Color = 7u16;
166-
167-
pub static BRIGHT_BLACK: Color = 8u16;
168-
pub static BRIGHT_RED: Color = 9u16;
169-
pub static BRIGHT_GREEN: Color = 10u16;
170-
pub static BRIGHT_YELLOW: Color = 11u16;
171-
pub static BRIGHT_BLUE: Color = 12u16;
172-
pub static BRIGHT_MAGENTA: Color = 13u16;
173-
pub static BRIGHT_CYAN: Color = 14u16;
174-
pub static BRIGHT_WHITE: Color = 15u16;
158+
pub const BLACK: Color = 0u16;
159+
pub const RED: Color = 1u16;
160+
pub const GREEN: Color = 2u16;
161+
pub const YELLOW: Color = 3u16;
162+
pub const BLUE: Color = 4u16;
163+
pub const MAGENTA: Color = 5u16;
164+
pub const CYAN: Color = 6u16;
165+
pub const WHITE: Color = 7u16;
166+
167+
pub const BRIGHT_BLACK: Color = 8u16;
168+
pub const BRIGHT_RED: Color = 9u16;
169+
pub const BRIGHT_GREEN: Color = 10u16;
170+
pub const BRIGHT_YELLOW: Color = 11u16;
171+
pub const BRIGHT_BLUE: Color = 12u16;
172+
pub const BRIGHT_MAGENTA: Color = 13u16;
173+
pub const BRIGHT_CYAN: Color = 14u16;
174+
pub const BRIGHT_WHITE: Color = 15u16;
175175
}
176176

177177
/// Terminal attributes

0 commit comments

Comments
 (0)