Skip to content

Commit 5c81a03

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 386c19d + 0c0a925 commit 5c81a03

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

chacha.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use core::prelude::*;
1414

1515
use {Rng, SeedableRng, Rand};
1616

17-
static KEY_WORDS : uint = 8; // 8 words for the 256-bit key
18-
static STATE_WORDS : uint = 16;
19-
static CHACHA_ROUNDS: uint = 20; // Cryptographically secure from 8 upwards as of this writing
17+
const KEY_WORDS : uint = 8; // 8 words for the 256-bit key
18+
const STATE_WORDS : uint = 16;
19+
const CHACHA_ROUNDS: uint = 20; // Cryptographically secure from 8 upwards as of this writing
2020

2121
/// A random number generator that uses the ChaCha20 algorithm [1].
2222
///

isaac.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use core::slice::raw;
1616

1717
use {Rng, SeedableRng, Rand};
1818

19-
static RAND_SIZE_LEN: u32 = 8;
20-
static RAND_SIZE: u32 = 1 << (RAND_SIZE_LEN as uint);
21-
static RAND_SIZE_UINT: uint = 1 << (RAND_SIZE_LEN as uint);
19+
const RAND_SIZE_LEN: u32 = 8;
20+
const RAND_SIZE: u32 = 1 << (RAND_SIZE_LEN as uint);
21+
const RAND_SIZE_UINT: uint = 1 << (RAND_SIZE_LEN as uint);
2222

2323
/// A random number generator that uses the ISAAC algorithm[1].
2424
///
@@ -251,8 +251,8 @@ impl Rand for IsaacRng {
251251
}
252252
}
253253

254-
static RAND_SIZE_64_LEN: uint = 8;
255-
static RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;
254+
const RAND_SIZE_64_LEN: uint = 8;
255+
const RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;
256256

257257
/// A random number generator that uses ISAAC-64[1], the 64-bit
258258
/// variant of the ISAAC algorithm.
@@ -356,8 +356,8 @@ impl Isaac64Rng {
356356
// abbreviations
357357
let mut a = self.a;
358358
let mut b = self.b + self.c;
359-
static MIDPOINT: uint = RAND_SIZE_64 / 2;
360-
static MP_VEC: [(uint, uint), .. 2] = [(0,MIDPOINT), (MIDPOINT, 0)];
359+
const MIDPOINT: uint = RAND_SIZE_64 / 2;
360+
const MP_VEC: [(uint, uint), .. 2] = [(0,MIDPOINT), (MIDPOINT, 0)];
361361
macro_rules! ind (
362362
($x:expr) => {
363363
*self.mem.unsafe_get(($x as uint >> 3) & (RAND_SIZE_64 - 1))

0 commit comments

Comments
 (0)