Skip to content

Commit f35f973

Browse files
committed
Use consts instead of statics where appropriate
This changes the type of some public constants/statics in libunicode. Notably some `&'static &'static [(char, char)]` have changed to `&'static [(char, char)]`. The regexp crate seems to be the sole user of these, yet this is technically a [breaking-change]
1 parent 1cc8b6e commit f35f973

File tree

53 files changed

+419
-424
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+419
-424
lines changed

src/compiletest/runtest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn run_rfail_test(config: &Config, props: &TestProps, testfile: &Path) {
127127
};
128128

129129
// The value our Makefile configures valgrind to return on failure
130-
static VALGRIND_ERR: int = 100;
130+
const VALGRIND_ERR: int = 100;
131131
if proc_res.status.matches_exit_status(VALGRIND_ERR) {
132132
fatal_proc_rec("run-fail test isn't valgrind-clean!", &proc_res);
133133
}
@@ -139,7 +139,7 @@ fn run_rfail_test(config: &Config, props: &TestProps, testfile: &Path) {
139139

140140
fn check_correct_failure_status(proc_res: &ProcRes) {
141141
// The value the rust runtime returns on failure
142-
static RUST_ERR: int = 101;
142+
const RUST_ERR: int = 101;
143143
if !proc_res.status.matches_exit_status(RUST_ERR) {
144144
fatal_proc_rec(
145145
&format!("failure produced the wrong error: {:?}",

src/compiletest/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use common::Config;
1414
use std::env;
1515

1616
/// Conversion table from triple OS name to Rust SYSNAME
17-
static OS_TABLE: &'static [(&'static str, &'static str)] = &[
17+
const OS_TABLE: &'static [(&'static str, &'static str)] = &[
1818
("mingw32", "windows"),
1919
("win32", "windows"),
2020
("windows", "windows"),

src/etc/unicode.py

+24-25
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ def emit_bsearch_range_table(f):
290290
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
291291
use core::cmp::Ordering::{Equal, Less, Greater};
292292
use core::slice::SliceExt;
293-
r.binary_search(|&(lo,hi)| {
293+
r.binary_search_by(|&(lo,hi)| {
294294
if lo <= c && c <= hi { Equal }
295295
else if hi < c { Less }
296296
else { Greater }
297-
}).found().is_some()
297+
}).is_ok()
298298
}\n
299299
""")
300300

@@ -303,7 +303,7 @@ def emit_table(f, name, t_data, t_type = "&'static [(char, char)]", is_pub=True,
303303
pub_string = ""
304304
if is_pub:
305305
pub_string = "pub "
306-
f.write(" %sstatic %s: %s = &[\n" % (pub_string, name, t_type))
306+
f.write(" %sconst %s: %s = &[\n" % (pub_string, name, t_type))
307307
data = ""
308308
first = True
309309
for dat in t_data:
@@ -329,14 +329,14 @@ def emit_property_module(f, mod, tbl, emit_fn):
329329
def emit_regex_module(f, cats, w_data):
330330
f.write("pub mod regex {\n")
331331
regex_class = "&'static [(char, char)]"
332-
class_table = "&'static [(&'static str, &'static %s)]" % regex_class
332+
class_table = "&'static [(&'static str, %s)]" % regex_class
333333

334334
emit_table(f, "UNICODE_CLASSES", cats, class_table,
335-
pfun=lambda x: "(\"%s\",&super::%s::%s_table)" % (x[0], x[1], x[0]))
335+
pfun=lambda x: "(\"%s\",super::%s::%s_table)" % (x[0], x[1], x[0]))
336336

337-
f.write(" pub static PERLD: &'static %s = &super::general_category::Nd_table;\n\n"
337+
f.write(" pub const PERLD: %s = super::general_category::Nd_table;\n\n"
338338
% regex_class)
339-
f.write(" pub static PERLS: &'static %s = &super::property::White_Space_table;\n\n"
339+
f.write(" pub const PERLS: %s = super::property::White_Space_table;\n\n"
340340
% regex_class)
341341

342342
emit_table(f, "PERLW", w_data, regex_class)
@@ -350,7 +350,7 @@ def emit_conversions_module(f, lowerupper, upperlower):
350350
use core::slice::SliceExt;
351351
use core::option::Option;
352352
use core::option::Option::{Some, None};
353-
use core::slice;
353+
use core::result::Result::{Ok, Err};
354354
355355
pub fn to_lower(c: char) -> char {
356356
match bsearch_case_table(c, LuLl_table) {
@@ -367,13 +367,13 @@ def emit_conversions_module(f, lowerupper, upperlower):
367367
}
368368
369369
fn bsearch_case_table(c: char, table: &'static [(char, char)]) -> Option<usize> {
370-
match table.binary_search(|&(key, _)| {
370+
match table.binary_search_by(|&(key, _)| {
371371
if c == key { Equal }
372372
else if key < c { Less }
373373
else { Greater }
374374
}) {
375-
slice::BinarySearchResult::Found(i) => Some(i),
376-
slice::BinarySearchResult::NotFound(_) => None,
375+
Ok(i) => Some(i),
376+
Err(_) => None,
377377
}
378378
}
379379
@@ -386,10 +386,9 @@ def emit_conversions_module(f, lowerupper, upperlower):
386386

387387
def emit_grapheme_module(f, grapheme_table, grapheme_cats):
388388
f.write("""pub mod grapheme {
389-
use core::kinds::Copy;
390389
use core::slice::SliceExt;
391390
pub use self::GraphemeCat::*;
392-
use core::slice;
391+
use core::result::Result::{Ok, Err};
393392
394393
#[allow(non_camel_case_types)]
395394
#[derive(Clone, Copy)]
@@ -401,16 +400,16 @@ def emit_grapheme_module(f, grapheme_table, grapheme_cats):
401400
402401
fn bsearch_range_value_table(c: char, r: &'static [(char, char, GraphemeCat)]) -> GraphemeCat {
403402
use core::cmp::Ordering::{Equal, Less, Greater};
404-
match r.binary_search(|&(lo, hi, _)| {
403+
match r.binary_search_by(|&(lo, hi, _)| {
405404
if lo <= c && c <= hi { Equal }
406405
else if hi < c { Less }
407406
else { Greater }
408407
}) {
409-
slice::BinarySearchResult::Found(idx) => {
408+
Ok(idx) => {
410409
let (_, _, cat) = r[idx];
411410
cat
412411
}
413-
slice::BinarySearchResult::NotFound(_) => GC_Any
412+
Err(_) => GC_Any
414413
}
415414
}
416415
@@ -430,20 +429,20 @@ def emit_charwidth_module(f, width_table):
430429
f.write(" use core::option::Option;\n")
431430
f.write(" use core::option::Option::{Some, None};\n")
432431
f.write(" use core::slice::SliceExt;\n")
433-
f.write(" use core::slice;\n")
432+
f.write(" use core::result::Result::{Ok, Err};\n")
434433
f.write("""
435434
fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 {
436435
use core::cmp::Ordering::{Equal, Less, Greater};
437-
match r.binary_search(|&(lo, hi, _, _)| {
436+
match r.binary_search_by(|&(lo, hi, _, _)| {
438437
if lo <= c && c <= hi { Equal }
439438
else if hi < c { Less }
440439
else { Greater }
441440
}) {
442-
slice::BinarySearchResult::Found(idx) => {
441+
Ok(idx) => {
443442
let (_, _, r_ncjk, r_cjk) = r[idx];
444443
if is_cjk { r_cjk } else { r_ncjk }
445444
}
446-
slice::BinarySearchResult::NotFound(_) => 1
445+
Err(_) => 1
447446
}
448447
}
449448
""")
@@ -530,17 +529,17 @@ def comp_pfun(char):
530529
fn bsearch_range_value_table(c: char, r: &'static [(char, char, u8)]) -> u8 {
531530
use core::cmp::Ordering::{Equal, Less, Greater};
532531
use core::slice::SliceExt;
533-
use core::slice;
534-
match r.binary_search(|&(lo, hi, _)| {
532+
use core::result::Result::{Ok, Err};
533+
match r.binary_search_by(|&(lo, hi, _)| {
535534
if lo <= c && c <= hi { Equal }
536535
else if hi < c { Less }
537536
else { Greater }
538537
}) {
539-
slice::BinarySearchResult::Found(idx) => {
538+
Ok(idx) => {
540539
let (_, _, result) = r[idx];
541540
result
542541
}
543-
slice::BinarySearchResult::NotFound(_) => 0
542+
Err(_) => 0
544543
}
545544
}\n
546545
""")
@@ -609,7 +608,7 @@ def optimize_width_table(wtable):
609608
unicode_version = re.search(pattern, readme.read()).groups()
610609
rf.write("""
611610
/// The version of [Unicode](http://www.unicode.org/)
612-
/// that the `UnicodeChar` and `UnicodeStrPrelude` traits are based on.
611+
/// that the unicode parts of `CharExt` and `UnicodeStrPrelude` traits are based on.
613612
pub const UNICODE_VERSION: (u64, u64, u64) = (%s, %s, %s);
614613
""" % unicode_version)
615614
(canon_decomp, compat_decomp, gencats, combines,

src/libcollections/bit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,7 @@ mod bit_vec_bench {
25442544

25452545
use super::BitVec;
25462546

2547-
static BENCH_BITS : usize = 1 << 14;
2547+
const BENCH_BITS : usize = 1 << 14;
25482548

25492549
fn rng() -> rand::IsaacRng {
25502550
let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
@@ -3039,7 +3039,7 @@ mod bit_set_bench {
30393039

30403040
use super::{BitVec, BitSet};
30413041

3042-
static BENCH_BITS : usize = 1 << 14;
3042+
const BENCH_BITS : usize = 1 << 14;
30433043

30443044
fn rng() -> rand::IsaacRng {
30453045
let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

src/libcollections/slice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,8 @@ fn insertion_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> O
13431343

13441344
fn merge_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Ordering {
13451345
// warning: this wildly uses unsafe.
1346-
static BASE_INSERTION: usize = 32;
1347-
static LARGE_INSERTION: usize = 16;
1346+
const BASE_INSERTION: usize = 32;
1347+
const LARGE_INSERTION: usize = 16;
13481348

13491349
// FIXME #12092: smaller insertion runs seems to make sorting
13501350
// vectors of large elements a little faster on some platforms,

src/libcollections/string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ impl String {
153153
}
154154
}
155155

156-
static TAG_CONT_U8: u8 = 128u8;
157-
static REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8
156+
const TAG_CONT_U8: u8 = 128u8;
157+
const REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8
158158
let total = v.len();
159159
fn unsafe_get(xs: &[u8], i: usize) -> u8 {
160160
unsafe { *xs.get_unchecked(i) }

src/libcollections/vec_deque.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use alloc::heap;
3939
#[unstable(feature = "collections")]
4040
pub use VecDeque as RingBuf;
4141

42-
static INITIAL_CAPACITY: usize = 7; // 2^3 - 1
43-
static MINIMUM_CAPACITY: usize = 1; // 2 - 1
42+
const INITIAL_CAPACITY: usize = 7; // 2^3 - 1
43+
const MINIMUM_CAPACITY: usize = 1; // 2 - 1
4444

4545
/// `VecDeque` is a growable ring buffer, which can be used as a
4646
/// double-ended queue efficiently.

src/libcore/fmt/float.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub enum SignFormat {
5353
SignNeg
5454
}
5555

56-
static DIGIT_E_RADIX: u32 = ('e' as u32) - ('a' as u32) + 11;
56+
const DIGIT_E_RADIX: u32 = ('e' as u32) - ('a' as u32) + 11;
5757

5858
/// Converts a number to its string representation as a byte vector.
5959
/// This is meant to be a common base implementation for all numeric string

src/libcoretest/num/int_macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ mod tests {
7070
assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
7171
}
7272

73-
static A: $T = 0b0101100;
74-
static B: $T = 0b0100001;
75-
static C: $T = 0b1111001;
73+
const A: $T = 0b0101100;
74+
const B: $T = 0b0100001;
75+
const C: $T = 0b1111001;
7676

77-
static _0: $T = 0;
78-
static _1: $T = !0;
77+
const _0: $T = 0;
78+
const _1: $T = !0;
7979

8080
#[test]
8181
fn test_count_ones() {

src/libcoretest/num/uint_macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ mod tests {
3838
assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
3939
}
4040

41-
static A: $T = 0b0101100;
42-
static B: $T = 0b0100001;
43-
static C: $T = 0b1111001;
41+
const A: $T = 0b0101100;
42+
const B: $T = 0b0100001;
43+
const C: $T = 0b1111001;
4444

45-
static _0: $T = 0;
46-
static _1: $T = !0;
45+
const _0: $T = 0;
46+
const _1: $T = !0;
4747

4848
#[test]
4949
fn test_count_ones() {

src/libflate/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ extern {
7373
-> *mut c_void;
7474
}
7575

76-
static LZ_NORM : c_int = 0x80; // LZ with 128 probes, "normal"
77-
static TINFL_FLAG_PARSE_ZLIB_HEADER : c_int = 0x1; // parse zlib header and adler32 checksum
78-
static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler32 checksum
76+
const LZ_NORM: c_int = 0x80; // LZ with 128 probes, "normal"
77+
const TINFL_FLAG_PARSE_ZLIB_HEADER: c_int = 0x1; // parse zlib header and adler32 checksum
78+
const TDEFL_WRITE_ZLIB_HEADER: c_int = 0x01000; // write zlib header and adler32 checksum
7979

8080
fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<Bytes> {
8181
unsafe {

src/librand/distributions/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ fn ziggurat<R: Rng, P, Z>(
223223
mut pdf: P,
224224
mut zero_case: Z)
225225
-> f64 where P: FnMut(f64) -> f64, Z: FnMut(&mut R, f64) -> f64 {
226-
static SCALE: f64 = (1u64 << 53) as f64;
226+
const SCALE: f64 = (1u64 << 53) as f64;
227227
loop {
228228
// reimplement the f64 generation as an optimisation suggested
229229
// by the Doornik paper: we have a lot of precision-space

src/librand/isaac.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl IsaacRng {
127127
let mut a = self.a;
128128
let mut b = self.b + self.c;
129129

130-
static MIDPOINT: uint = (RAND_SIZE / 2) as uint;
130+
const MIDPOINT: uint = (RAND_SIZE / 2) as uint;
131131

132132
macro_rules! ind {
133133
($x:expr) => ( self.mem[(($x >> 2) as uint & ((RAND_SIZE - 1) as uint))] )

src/librand/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use distributions::{Range, IndependentSample};
5252
use distributions::range::SampleRange;
5353

5454
#[cfg(test)]
55-
static RAND_BENCH_N: u64 = 100;
55+
const RAND_BENCH_N: u64 = 100;
5656

5757
pub mod distributions;
5858
pub mod isaac;
@@ -342,7 +342,7 @@ impl<'a, R: Rng> Iterator for AsciiGenerator<'a, R> {
342342
type Item = char;
343343

344344
fn next(&mut self) -> Option<char> {
345-
static GEN_ASCII_STR_CHARSET: &'static [u8] =
345+
const GEN_ASCII_STR_CHARSET: &'static [u8] =
346346
b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
347347
abcdefghijklmnopqrstuvwxyz\
348348
0123456789";

src/librand/rand_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl Rand for char {
141141
#[inline]
142142
fn rand<R: Rng>(rng: &mut R) -> char {
143143
// a char is 21 bits
144-
static CHAR_MASK: u32 = 0x001f_ffff;
144+
const CHAR_MASK: u32 = 0x001f_ffff;
145145
loop {
146146
// Rejection sampling. About 0.2% of numbers with at most
147147
// 21-bits are invalid codepoints (surrogates), so this

src/librand/reseeding.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core::default::Default;
1818

1919
/// How many bytes of entropy the underling RNG is allowed to generate
2020
/// before it is reseeded.
21-
static DEFAULT_GENERATION_THRESHOLD: uint = 32 * 1024;
21+
const DEFAULT_GENERATION_THRESHOLD: uint = 32 * 1024;
2222

2323
/// A wrapper around any RNG which reseeds the underlying RNG after it
2424
/// has generated a certain number of random bytes.
@@ -212,7 +212,7 @@ mod test {
212212
assert_eq!(string1, string2);
213213
}
214214

215-
static FILL_BYTES_V_LEN: uint = 13579;
215+
const FILL_BYTES_V_LEN: uint = 13579;
216216
#[test]
217217
fn test_rng_fill_bytes() {
218218
let mut v = repeat(0u8).take(FILL_BYTES_V_LEN).collect::<Vec<_>>();

src/librbml/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::old_io;
1313
use std::slice;
1414
use std::iter::repeat;
1515

16-
static BUF_CAPACITY: uint = 128;
16+
const BUF_CAPACITY: uint = 128;
1717

1818
fn combine(seek: SeekStyle, cur: uint, end: uint, offset: i64) -> IoResult<u64> {
1919
// compute offset as signed and clamp to prevent overflow

src/librbml/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -856,9 +856,9 @@ pub mod writer {
856856
// Set to true to generate more debugging in EBML code.
857857
// Totally lame approach.
858858
#[cfg(not(ndebug))]
859-
static DEBUG: bool = true;
859+
const DEBUG: bool = true;
860860
#[cfg(ndebug)]
861-
static DEBUG: bool = false;
861+
const DEBUG: bool = false;
862862

863863
impl<'a, W: Writer + Seek> Encoder<'a, W> {
864864
// used internally to emit things like the vector length and so on

src/librustc/metadata/filesearch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ pub fn get_or_default_sysroot() -> Path {
202202
}
203203

204204
#[cfg(windows)]
205-
static PATH_ENTRY_SEPARATOR: &'static str = ";";
205+
const PATH_ENTRY_SEPARATOR: char = ';';
206206
#[cfg(not(windows))]
207-
static PATH_ENTRY_SEPARATOR: &'static str = ":";
207+
const PATH_ENTRY_SEPARATOR: char = ':';
208208

209209
/// Returns RUST_PATH as a string, without default paths added
210210
pub fn get_rust_path() -> Option<String> {

0 commit comments

Comments
 (0)