From 6d8621ae7f0df0bc3878c256b98a6483fc3d9a32 Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Sun, 23 Dec 2012 14:37:44 -0800 Subject: [PATCH 1/2] core: Mark some functions as pure --- src/libcore/dvec.rs | 6 +++--- src/libcore/float.rs | 4 ++-- src/libcore/int-template/int.rs | 4 ++-- src/libcore/ptr.rs | 4 ++-- src/libcore/rand.rs | 4 ++-- src/libcore/str.rs | 6 ++---- src/libcore/uint-template/uint.rs | 2 +- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/libcore/dvec.rs b/src/libcore/dvec.rs index b02a6f5c8b5..236d6bce9f0 100644 --- a/src/libcore/dvec.rs +++ b/src/libcore/dvec.rs @@ -67,17 +67,17 @@ pub pure fn DVec() -> DVec { } /// Creates a new dvec with a single element -pub fn from_elem(e: A) -> DVec { +pub pure fn from_elem(e: A) -> DVec { DVec {mut data: ~[move e]} } /// Creates a new dvec with the contents of a vector -pub fn from_vec(v: ~[A]) -> DVec { +pub pure fn from_vec(v: ~[A]) -> DVec { DVec {mut data: move v} } /// Consumes the vector and returns its contents -pub fn unwrap(d: DVec) -> ~[A] { +pub pure fn unwrap(d: DVec) -> ~[A] { let DVec {data: v} = move d; move v } diff --git a/src/libcore/float.rs b/src/libcore/float.rs index 27184a39708..02ba4419bcc 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -188,7 +188,7 @@ pub pure fn to_str_common(num: float, digits: uint, exact: bool) -> ~str { * * num - The float value * * digits - The number of significant digits */ -pub fn to_str_exact(num: float, digits: uint) -> ~str { +pub pure fn to_str_exact(num: float, digits: uint) -> ~str { to_str_common(num, digits, true) } @@ -238,7 +238,7 @@ pub pure fn to_str(num: float, digits: uint) -> ~str { * `none` if the string did not represent a valid number. Otherwise, * `Some(n)` where `n` is the floating-point number represented by `[num]`. */ -pub fn from_str(num: &str) -> Option { +pub pure fn from_str(num: &str) -> Option { if num == "inf" { return Some(infinity as float); } else if num == "-inf" { diff --git a/src/libcore/int-template/int.rs b/src/libcore/int-template/int.rs index c281185c75e..61a7c3bd07a 100644 --- a/src/libcore/int-template/int.rs +++ b/src/libcore/int-template/int.rs @@ -17,12 +17,12 @@ mod inst { pub const bits: uint = uint::bits; /// Returns `base` raised to the power of `exponent` - pub fn pow(base: int, exponent: uint) -> int { + pub pure fn pow(base: int, exponent: uint) -> int { if exponent == 0u { //Not mathemtically true if ~[base == 0] return 1; } - if base == 0 { return 0; } + if base == 0 { return 0; } let mut my_pow = exponent; let mut acc = 1; let mut multiplier = base; diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 0e83157c82d..c838af700f6 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -170,13 +170,13 @@ pub pure fn to_mut_unsafe_ptr(thing: &mut T) -> *mut T { (I couldn't think of a cutesy name for this one.) */ #[inline(always)] -pub fn to_uint(thing: &T) -> uint unsafe { +pub pure fn to_uint(thing: &T) -> uint unsafe { cast::reinterpret_cast(&thing) } /// Determine if two borrowed pointers point to the same thing. #[inline(always)] -pub fn ref_eq(thing: &a/T, other: &b/T) -> bool { +pub pure fn ref_eq(thing: &a/T, other: &b/T) -> bool { to_uint(thing) == to_uint(other) } diff --git a/src/libcore/rand.rs b/src/libcore/rand.rs index 96caf062d27..8ee857ef927 100644 --- a/src/libcore/rand.rs +++ b/src/libcore/rand.rs @@ -309,12 +309,12 @@ impl XorShiftState: Rng { } } -pub fn xorshift() -> Rng { +pub pure fn xorshift() -> Rng { // constants taken from http://en.wikipedia.org/wiki/Xorshift seeded_xorshift(123456789u32, 362436069u32, 521288629u32, 88675123u32) } -pub fn seeded_xorshift(x: u32, y: u32, z: u32, w: u32) -> Rng { +pub pure fn seeded_xorshift(x: u32, y: u32, z: u32, w: u32) -> Rng { {mut x: x, mut y: y, mut z: z, mut w: w} as Rng } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 1154a86f96c..e68966945ca 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -214,7 +214,7 @@ pub pure fn connect(v: &[~str], sep: &str) -> ~str { } /// Given a string, make a new string with repeated copies of it -pub fn repeat(ss: &str, nn: uint) -> ~str { +pub pure fn repeat(ss: &str, nn: uint) -> ~str { let mut acc = ~""; for nn.times { acc += ss; } acc @@ -1684,9 +1684,7 @@ pub struct CharRange { * * This function can be used to iterate over a unicode string in reverse. */ -pure fn char_range_at_reverse(ss: &str, start: uint) - -> CharRange { - +pure fn char_range_at_reverse(ss: &str, start: uint) -> CharRange { let mut prev = start; // while there is a previous byte == 10...... diff --git a/src/libcore/uint-template/uint.rs b/src/libcore/uint-template/uint.rs index bd7f8c09f47..b2ae1aa921b 100644 --- a/src/libcore/uint-template/uint.rs +++ b/src/libcore/uint-template/uint.rs @@ -104,7 +104,7 @@ mod inst { /// Returns the smallest power of 2 greater than or equal to `n` #[inline(always)] - pub fn next_power_of_two(n: uint) -> uint { + pub pure fn next_power_of_two(n: uint) -> uint { let halfbits: uint = sys::size_of::() * 4u; let mut tmp: uint = n - 1u; let mut shift: uint = 1u; From ffaa47736866ca89b8d23cd3f14d54efefdb6243 Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Sun, 23 Dec 2012 14:38:01 -0800 Subject: [PATCH 2/2] std: Mark some functions as pure --- src/libstd/arena.rs | 2 +- src/libstd/c_vec.rs | 2 +- src/libstd/list.rs | 12 ++++++------ src/libstd/rope.rs | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/libstd/arena.rs b/src/libstd/arena.rs index 105e8fb122f..9054f9355ad 100644 --- a/src/libstd/arena.rs +++ b/src/libstd/arena.rs @@ -95,7 +95,7 @@ pub fn Arena() -> Arena { } #[inline(always)] -fn round_up_to(base: uint, align: uint) -> uint { +pure fn round_up_to(base: uint, align: uint) -> uint { (base + (align - 1)) & !(align - 1) } diff --git a/src/libstd/c_vec.rs b/src/libstd/c_vec.rs index cd1fa33a08f..cc8cadb709d 100644 --- a/src/libstd/c_vec.rs +++ b/src/libstd/c_vec.rs @@ -135,7 +135,7 @@ pub fn set(t: CVec, ofs: uint, v: T) { */ /// Returns the length of the vector -pub fn len(t: CVec) -> uint { +pub pure fn len(t: CVec) -> uint { return (*t).len; } diff --git a/src/libstd/list.rs b/src/libstd/list.rs index 6d2c10eb827..e41aab8ec1f 100644 --- a/src/libstd/list.rs +++ b/src/libstd/list.rs @@ -22,8 +22,8 @@ pub enum List { Nil, } -/// Cregate a list from a vector -pub fn from_vec(v: &[T]) -> @List { +/// Create a list from a vector +pub pure fn from_vec(v: &[T]) -> @List { vec::foldr(v, @Nil::, |h, t| @Cons(*h, t)) } @@ -53,7 +53,7 @@ pub fn foldl(z: T, ls: @List, f: fn(&T, &U) -> T) -> T { * When function `f` returns true then an option containing the element * is returned. If `f` matches no elements then none is returned. */ -pub fn find(ls: @List, f: fn(&T) -> bool) -> Option { +pub pure fn find(ls: @List, f: fn(&T) -> bool) -> Option { let mut ls = ls; loop { ls = match *ls { @@ -88,7 +88,7 @@ pub pure fn is_not_empty(ls: @List) -> bool { } /// Returns the length of a list -pub fn len(ls: @List) -> uint { +pub pure fn len(ls: @List) -> uint { let mut count = 0u; iter(ls, |_e| count += 1u); count @@ -131,7 +131,7 @@ pure fn push(ll: &mut @list, vv: T) { */ /// Iterate over a list -pub fn iter(l: @List, f: fn(&T)) { +pub pure fn iter(l: @List, f: fn(&T)) { let mut cur = l; loop { cur = match *cur { @@ -145,7 +145,7 @@ pub fn iter(l: @List, f: fn(&T)) { } /// Iterate over a list -pub fn each(l: @List, f: fn(&T) -> bool) { +pub pure fn each(l: @List, f: fn(&T) -> bool) { let mut cur = l; loop { cur = match *cur { diff --git a/src/libstd/rope.rs b/src/libstd/rope.rs index 473992e6820..aa78d22e4c8 100644 --- a/src/libstd/rope.rs +++ b/src/libstd/rope.rs @@ -43,7 +43,7 @@ pub type Rope = node::Root; */ /// Create an empty rope -pub fn empty() -> Rope { +pub pure fn empty() -> Rope { return node::Empty; } @@ -479,7 +479,7 @@ pub mod iterator { * * Constant time. */ -pub fn height(rope: Rope) -> uint { +pub pure fn height(rope: Rope) -> uint { match (rope) { node::Empty => return 0u, node::Content(x) => return node::height(x) @@ -1019,7 +1019,7 @@ mod node { }) } - pub fn height(node: @Node) -> uint { + pub pure fn height(node: @Node) -> uint { match (*node) { Leaf(_) => return 0u, Concat(ref x) => return x.height @@ -1100,7 +1100,7 @@ mod node { * proportional to the height of the rope + the (bounded) * length of the largest leaf. */ - pub fn char_at(node: @Node, pos: uint) -> char { + pub pure fn char_at(node: @Node, pos: uint) -> char { let mut node = node; let mut pos = pos; loop {