1
Fork 0

Removing no longer needed unsafe blocks

This commit is contained in:
Alex Crichton 2013-04-08 16:50:34 -04:00
parent 3136fba5ae
commit 255193cc1a
17 changed files with 373 additions and 477 deletions

View file

@ -202,12 +202,10 @@ pub fn escape_unicode(c: char) -> ~str {
else { ('U', 8u) });
assert!(str::len(s) <= pad);
let mut out = ~"\\";
unsafe {
str::push_str(&mut out, str::from_char(c));
for uint::range(str::len(s), pad) |_i|
{ str::push_str(&mut out, ~"0"); }
str::push_str(&mut out, s);
}
out
}

View file

@ -76,36 +76,30 @@ pub trait Streaming {
impl<A:IterBytes> Hash for A {
#[inline(always)]
fn hash_keyed(&self, k0: u64, k1: u64) -> u64 {
unsafe {
let s = &State(k0, k1);
for self.iter_bytes(true) |bytes| {
s.input(bytes);
}
s.result_u64()
}
}
}
fn hash_keyed_2<A: IterBytes,
B: IterBytes>(a: &A, b: &B, k0: u64, k1: u64) -> u64 {
unsafe {
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}
}
fn hash_keyed_3<A: IterBytes,
B: IterBytes,
C: IterBytes>(a: &A, b: &B, c: &C, k0: u64, k1: u64) -> u64 {
unsafe {
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
for c.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}
}
fn hash_keyed_4<A: IterBytes,
@ -113,14 +107,12 @@ fn hash_keyed_4<A: IterBytes,
C: IterBytes,
D: IterBytes>(a: &A, b: &B, c: &C, d: &D, k0: u64, k1: u64)
-> u64 {
unsafe {
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
for c.iter_bytes(true) |bytes| { s.input(bytes); }
for d.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}
}
fn hash_keyed_5<A: IterBytes,
@ -129,7 +121,6 @@ fn hash_keyed_5<A: IterBytes,
D: IterBytes,
E: IterBytes>(a: &A, b: &B, c: &C, d: &D, e: &E,
k0: u64, k1: u64) -> u64 {
unsafe {
let s = &State(k0, k1);
for a.iter_bytes(true) |bytes| { s.input(bytes); }
for b.iter_bytes(true) |bytes| { s.input(bytes); }
@ -137,7 +128,6 @@ fn hash_keyed_5<A: IterBytes,
for d.iter_bytes(true) |bytes| { s.input(bytes); }
for e.iter_bytes(true) |bytes| { s.input(bytes); }
s.result_u64()
}
}
// Implement State as SipState

View file

@ -228,10 +228,8 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
deccum /= radix_gen;
deccum = deccum.round_to_zero();
unsafe { // FIXME: Pureness workaround (#4568)
buf.push(char::from_digit(current_digit.to_int() as uint, radix)
.unwrap() as u8);
}
// No more digits to calculate for the non-fractional part -> break
if deccum == _0 { break; }
@ -247,21 +245,15 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
// Decide what sign to put in front
match sign {
SignNeg | SignAll if neg => {
unsafe { // FIXME: Pureness workaround (#4568)
buf.push('-' as u8);
}
}
SignAll => {
unsafe { // FIXME: Pureness workaround (#4568)
buf.push('+' as u8);
}
}
_ => ()
}
unsafe { // FIXME: Pureness workaround (#4568)
vec::reverse(buf);
}
// Remember start of the fractional digits.
// Points one beyond end of buf if none get generated,
@ -271,9 +263,7 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
// Now emit the fractional part, if any
deccum = num.fractional_part();
if deccum != _0 || (limit_digits && exact && digit_count > 0) {
unsafe { // FIXME: Pureness workaround (#4568)
buf.push('.' as u8);
}
let mut dig = 0u;
// calculate new digits while
@ -299,10 +289,8 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
current_digit_signed
};
unsafe { // FIXME: Pureness workaround (#4568)
buf.push(char::from_digit(
current_digit.to_int() as uint, radix).unwrap() as u8);
}
// Decrease the deccumulator one fractional digit at a time
deccum = deccum.fractional_part();
@ -320,7 +308,6 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
char::from_digit(val, radix).unwrap() as u8
};
unsafe { // FIXME: Pureness workaround (#4568)
let extra_digit = ascii2value(buf.pop());
if extra_digit >= radix / 2 { // -> need to round
let mut i: int = buf.len() as int - 1;
@ -351,7 +338,6 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
}
}
}
}
// if number of digits is not exact, remove all trailing '0's up to
// and including the '.'

View file

@ -39,10 +39,9 @@ pub enum Result<T, U> {
pub fn get<T:Copy,U>(res: &Result<T, U>) -> T {
match *res {
Ok(copy t) => t,
Err(ref the_err) => unsafe {
Err(ref the_err) =>
fail!(fmt!("get called on error result: %?", *the_err))
}
}
}
/**
@ -56,10 +55,9 @@ pub fn get<T:Copy,U>(res: &Result<T, U>) -> T {
pub fn get_ref<'a, T, U>(res: &'a Result<T, U>) -> &'a T {
match *res {
Ok(ref t) => t,
Err(ref the_err) => unsafe {
Err(ref the_err) =>
fail!(fmt!("get_ref called on error result: %?", *the_err))
}
}
}
/**

View file

@ -1020,12 +1020,10 @@ pub fn any(ss: &str, pred: &fn(char) -> bool) -> bool {
/// Apply a function to each character
pub fn map(ss: &str, ff: &fn(char) -> char) -> ~str {
let mut result = ~"";
unsafe {
reserve(&mut result, len(ss));
for ss.each_char |cc| {
str::push_char(&mut result, ff(cc));
}
}
result
}
@ -1660,7 +1658,6 @@ pub fn to_utf16(s: &str) -> ~[u16] {
// Arithmetic with u32 literals is easier on the eyes than chars.
let mut ch = ch as u32;
unsafe {
if (ch & 0xFFFF_u32) == ch {
// The BMP falls through (assuming non-surrogate, as it
// should)
@ -1675,7 +1672,6 @@ pub fn to_utf16(s: &str) -> ~[u16] {
u.push_all(~[w1, w2])
}
}
}
u
}
@ -1705,16 +1701,14 @@ pub fn utf16_chars(v: &[u16], f: &fn(char)) {
pub fn from_utf16(v: &[u16]) -> ~str {
let mut buf = ~"";
unsafe {
reserve(&mut buf, vec::len(v));
utf16_chars(v, |ch| push_char(&mut buf, ch));
}
buf
}
pub fn with_capacity(capacity: uint) -> ~str {
let mut buf = ~"";
unsafe { reserve(&mut buf, capacity); }
reserve(&mut buf, capacity);
buf
}
@ -2105,24 +2099,20 @@ pub fn capacity(s: &const ~str) -> uint {
/// Escape each char in `s` with char::escape_default.
pub fn escape_default(s: &str) -> ~str {
let mut out: ~str = ~"";
unsafe {
reserve_at_least(&mut out, str::len(s));
for s.each_char |c| {
push_str(&mut out, char::escape_default(c));
}
}
out
}
/// Escape each char in `s` with char::escape_unicode.
pub fn escape_unicode(s: &str) -> ~str {
let mut out: ~str = ~"";
unsafe {
reserve_at_least(&mut out, str::len(s));
for s.each_char |c| {
push_str(&mut out, char::escape_unicode(c));
}
}
out
}

View file

@ -72,64 +72,43 @@ impl<A:ToStr,B:ToStr,C:ToStr> ToStr for (A, B, C) {
impl<'self,A:ToStr> ToStr for &'self [A] {
#[inline(always)]
fn to_str(&self) -> ~str {
unsafe {
// FIXME #4568
// Bleh -- not really unsafe
// push_str and push_char
let mut acc = ~"[", first = true;
for self.each |elt| {
unsafe {
if first { first = false; }
else { str::push_str(&mut acc, ~", "); }
str::push_str(&mut acc, elt.to_str());
}
}
str::push_char(&mut acc, ']');
acc
}
}
}
impl<A:ToStr> ToStr for ~[A] {
#[inline(always)]
fn to_str(&self) -> ~str {
unsafe {
// FIXME #4568
// Bleh -- not really unsafe
// push_str and push_char
let mut acc = ~"[", first = true;
for self.each |elt| {
unsafe {
if first { first = false; }
else { str::push_str(&mut acc, ~", "); }
str::push_str(&mut acc, elt.to_str());
}
}
str::push_char(&mut acc, ']');
acc
}
}
}
impl<A:ToStr> ToStr for @[A] {
#[inline(always)]
fn to_str(&self) -> ~str {
unsafe {
// FIXME #4568
// Bleh -- not really unsafe
// push_str and push_char
let mut acc = ~"[", first = true;
for self.each |elt| {
unsafe {
if first { first = false; }
else { str::push_str(&mut acc, ~", "); }
str::push_str(&mut acc, elt.to_str());
}
}
str::push_char(&mut acc, ']');
acc
}
}
}
#[cfg(test)]

View file

@ -167,7 +167,7 @@ pub fn from_slice<T:Copy>(t: &[T]) -> ~[T] {
pub fn with_capacity<T>(capacity: uint) -> ~[T] {
let mut vec = ~[];
unsafe { reserve(&mut vec, capacity); }
reserve(&mut vec, capacity);
vec
}
@ -186,7 +186,7 @@ pub fn with_capacity<T>(capacity: uint) -> ~[T] {
#[inline(always)]
pub fn build_sized<A>(size: uint, builder: &fn(push: &fn(v: A))) -> ~[A] {
let mut vec = with_capacity(size);
builder(|x| unsafe { vec.push(x) });
builder(|x| vec.push(x));
vec
}
@ -437,14 +437,12 @@ pub fn partitioned<T:Copy>(v: &[T], f: &fn(&T) -> bool) -> (~[T], ~[T]) {
let mut rights = ~[];
for each(v) |elt| {
unsafe {
if f(elt) {
lefts.push(*elt);
} else {
rights.push(*elt);
}
}
}
(lefts, rights)
}
@ -735,16 +733,14 @@ pub fn dedup<T:Eq>(v: &mut ~[T]) {
#[inline(always)]
pub fn append<T:Copy>(lhs: ~[T], rhs: &const [T]) -> ~[T] {
let mut v = lhs;
unsafe {
v.push_all(rhs);
}
v
}
#[inline(always)]
pub fn append_one<T>(lhs: ~[T], x: T) -> ~[T] {
let mut v = lhs;
unsafe { v.push(x); }
v.push(x);
v
}
@ -811,10 +807,8 @@ pub fn grow_set<T:Copy>(v: &mut ~[T], index: uint, initval: &T, val: T) {
pub fn map<T, U>(v: &[T], f: &fn(t: &T) -> U) -> ~[U] {
let mut result = with_capacity(len(v));
for each(v) |elem| {
unsafe {
result.push(f(elem));
}
}
result
}
@ -841,7 +835,7 @@ pub fn mapi<T, U>(v: &[T], f: &fn(uint, t: &T) -> U) -> ~[U] {
*/
pub fn flat_map<T, U>(v: &[T], f: &fn(t: &T) -> ~[U]) -> ~[U] {
let mut result = ~[];
for each(v) |elem| { unsafe{ result.push_all_move(f(elem)); } }
for each(v) |elem| { result.push_all_move(f(elem)); }
result
}
@ -853,7 +847,7 @@ pub fn map2<T:Copy,U:Copy,V>(v0: &[T], v1: &[U],
let mut u: ~[V] = ~[];
let mut i = 0u;
while i < v0_len {
unsafe { u.push(f(&v0[i], &v1[i])) };
u.push(f(&v0[i], &v1[i]));
i += 1u;
}
u
@ -894,7 +888,7 @@ pub fn filter_mapped<T, U: Copy>(
for each(v) |elem| {
match f(elem) {
None => {/* no-op */ }
Some(result_elem) => unsafe { result.push(result_elem); }
Some(result_elem) => { result.push(result_elem); }
}
}
result
@ -927,7 +921,7 @@ pub fn filter<T>(v: ~[T], f: &fn(t: &T) -> bool) -> ~[T] {
pub fn filtered<T:Copy>(v: &[T], f: &fn(t: &T) -> bool) -> ~[T] {
let mut result = ~[];
for each(v) |elem| {
if f(elem) { unsafe { result.push(*elem); } }
if f(elem) { result.push(*elem); }
}
result
}
@ -959,7 +953,7 @@ pub fn retain<T>(v: &mut ~[T], f: &fn(t: &T) -> bool) {
*/
pub fn concat<T:Copy>(v: &[~[T]]) -> ~[T] {
let mut r = ~[];
for each(v) |inner| { unsafe { r.push_all(*inner); } }
for each(v) |inner| { r.push_all(*inner); }
r
}
@ -968,8 +962,8 @@ pub fn connect<T:Copy>(v: &[~[T]], sep: &T) -> ~[T] {
let mut r: ~[T] = ~[];
let mut first = true;
for each(v) |inner| {
if first { first = false; } else { unsafe { r.push(*sep); } }
unsafe { r.push_all(*inner) };
if first { first = false; } else { r.push(*sep); }
r.push_all(*inner);
}
r
}
@ -1236,11 +1230,9 @@ pub fn unzip_slice<T:Copy,U:Copy>(v: &[(T, U)]) -> (~[T], ~[U]) {
let mut ts = ~[], us = ~[];
for each(v) |p| {
let (t, u) = *p;
unsafe {
ts.push(t);
us.push(u);
}
}
(ts, us)
}
@ -1254,13 +1246,11 @@ pub fn unzip_slice<T:Copy,U:Copy>(v: &[(T, U)]) -> (~[T], ~[U]) {
*/
pub fn unzip<T,U>(v: ~[(T, U)]) -> (~[T], ~[U]) {
let mut ts = ~[], us = ~[];
unsafe {
do consume(v) |_i, p| {
let (t, u) = p;
ts.push(t);
us.push(u);
}
}
(ts, us)
}
@ -1274,7 +1264,8 @@ pub fn zip_slice<T:Copy,U:Copy>(v: &const [T], u: &const [U])
let mut i = 0u;
assert!(sz == len(u));
while i < sz {
unsafe { zipped.push((v[i], u[i])); i += 1u; }
zipped.push((v[i], u[i]));
i += 1u;
}
zipped
}
@ -1290,10 +1281,10 @@ pub fn zip<T, U>(mut v: ~[T], mut u: ~[U]) -> ~[(T, U)] {
assert!(i == len(u));
let mut w = with_capacity(i);
while i > 0 {
unsafe { w.push((v.pop(),u.pop())); }
w.push((v.pop(),u.pop()));
i -= 1;
}
unsafe { reverse(w); }
reverse(w);
w
}
@ -1322,10 +1313,8 @@ pub fn reversed<T:Copy>(v: &const [T]) -> ~[T] {
let mut rs: ~[T] = ~[];
let mut i = len::<T>(v);
if i == 0 { return (rs); } else { i -= 1; }
unsafe {
while i != 0 { rs.push(v[i]); i -= 1; }
rs.push(v[0]);
}
rs
}
@ -1495,14 +1484,12 @@ pub fn each_permutation<T:Copy>(v: &[T], put: &fn(ts: &[T]) -> bool) {
while i < ln {
let elt = v[i];
let mut rest = slice(v, 0u, i).to_vec();
unsafe {
rest.push_all(const_slice(v, i+1u, ln));
for each_permutation(rest) |permutation| {
if !put(append(~[elt], permutation)) {
return;
}
}
}
i += 1u;
}
}
@ -1514,11 +1501,9 @@ pub fn windowed<TT:Copy>(nn: uint, xx: &[TT]) -> ~[~[TT]] {
for vec::eachi (xx) |ii, _x| {
let len = xx.len();
if ii+nn <= len {
unsafe {
ww.push(slice(xx, ii, ii+nn).to_vec());
}
}
}
ww
}

View file

@ -29,7 +29,6 @@ static CHARS: [char, ..64] = [
impl<'self> ToBase64 for &'self [u8] {
fn to_base64(&self) -> ~str {
let mut s = ~"";
unsafe {
let len = self.len();
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
@ -70,7 +69,6 @@ impl<'self> ToBase64 for &'self [u8] {
}
_ => fail!(~"Algebra is broken, please alert the math police")
}
}
s
}
}
@ -99,7 +97,6 @@ impl FromBase64 for ~[u8] {
let mut r = vec::with_capacity((len / 4u) * 3u - padding);
unsafe {
let mut i = 0u;
while i < len {
let mut n = 0u;
@ -142,7 +139,6 @@ impl FromBase64 for ~[u8] {
r.push(((n >> 8u ) & 0xFFu) as u8);
r.push(((n ) & 0xFFu) as u8);
}
}
r
}
}

View file

@ -99,7 +99,7 @@ pub fn DList<T>() -> @mut DList<T> {
/// Creates a new dlist with a single element
pub fn from_elem<T>(data: T) -> @mut DList<T> {
let list = DList();
unsafe { list.push(data); }
list.push(data);
list
}
@ -484,12 +484,9 @@ pub impl<T:Copy> DList<T> {
/// Get the elements of the list as a vector. O(n).
fn to_vec(@mut self) -> ~[T] {
let mut v = vec::with_capacity(self.size);
unsafe {
// Take this out of the unchecked when iter's functions are pure
for iter::eachi(&self) |index,data| {
v[index] = *data;
}
}
v
}
}

View file

@ -342,10 +342,7 @@ pub fn to_writer(wr: @io::Writer, json: &Json) {
/// Encodes a json value into a string
pub fn to_str(json: &Json) -> ~str {
unsafe {
// ugh, should be safe
io::with_str_writer(|wr| to_writer(wr, json))
}
}
/// Encodes a json value into a io::writer
@ -988,7 +985,6 @@ impl Ord for Json {
match *other {
Number(_) | String(_) | Boolean(_) | List(_) => false,
Object(ref d1) => {
unsafe {
let mut d0_flat = ~[];
let mut d1_flat = ~[];
@ -1005,7 +1001,6 @@ impl Ord for Json {
d0_flat < d1_flat
}
}
Null => true
}
}

View file

@ -29,14 +29,14 @@ pub fn md4(msg: &[u8]) -> Quad {
let mut msg = vec::append(vec::from_slice(msg), ~[0x80u8]);
let mut bitlen = orig_len + 8u64;
while (bitlen + 64u64) % 512u64 > 0u64 {
unsafe {msg.push(0u8);}
msg.push(0u8);
bitlen += 8u64;
}
// append length
let mut i = 0u64;
while i < 8u64 {
unsafe {msg.push((orig_len >> (i * 8u64)) as u8);}
msg.push((orig_len >> (i * 8u64)) as u8);
i += 1u64;
}

View file

@ -341,7 +341,7 @@ pub impl BigUint {
if new_len == v.len() { return BigUint { data: v }; }
let mut v = v;
unsafe { v.truncate(new_len); }
v.truncate(new_len);
return BigUint { data: v };
}

View file

@ -283,7 +283,6 @@ mod tests {
#[test]
pub fn test() {
unsafe {
struct Test {
input: ~str,
output: ~[u8],
@ -412,7 +411,6 @@ mod tests {
sh.reset();
}
}
}
}
// Local Variables:

View file

@ -27,7 +27,7 @@ type Le<'self, T> = &'self fn(v1: &T, v2: &T) -> bool;
pub fn merge_sort<T:Copy>(v: &const [T], le: Le<T>) -> ~[T] {
type Slice = (uint, uint);
unsafe {return merge_sort_(v, (0u, len(v)), le);}
return merge_sort_(v, (0u, len(v)), le);
fn merge_sort_<T:Copy>(v: &const [T], slice: Slice, le: Le<T>)
-> ~[T] {
@ -68,15 +68,12 @@ fn part<T>(arr: &mut [T], left: uint,
let mut storage_index: uint = left;
let mut i: uint = left;
while i < right {
// XXX: Unsafe because borrow check doesn't handle this right
unsafe {
let a: &T = cast::transmute(&mut arr[i]);
let b: &T = cast::transmute(&mut arr[right]);
let a: &mut T = &mut arr[i];
let b: &mut T = &mut arr[right];
if compare_func(a, b) {
arr[i] <-> arr[storage_index];
storage_index += 1;
}
}
i += 1;
}
arr[storage_index] <-> arr[right];
@ -887,14 +884,11 @@ mod tests {
pub fn test_merge_sort_stability() {
// tjc: funny that we have to use parens
fn ile(x: &(&'static str), y: &(&'static str)) -> bool
{
unsafe // to_lower is not pure...
{
let x = x.to_lower();
let y = y.to_lower();
x <= y
}
}
let names1 = ~["joe bob", "Joe Bob", "Jack Brown", "JOE Bob",
"Sally Mae", "JOE BOB", "Alex Andy"];
@ -921,10 +915,8 @@ mod test_tim_sort {
impl Ord for CVal {
fn lt(&self, other: &CVal) -> bool {
unsafe {
let rng = rand::Rng();
if rng.gen_float() > 0.995 { fail!(~"It's happening!!!"); }
}
(*self).val < other.val
}
fn le(&self, other: &CVal) -> bool { (*self).val <= other.val }

View file

@ -176,16 +176,12 @@ pub fn now() -> Tm {
/// Parses the time from the string according to the format string.
pub fn strptime(s: &str, format: &str) -> Result<Tm, ~str> {
// unsafe only because do_strptime is annoying to make pure
// (it does IO with a str_reader)
unsafe {do_strptime(s, format)}
do_strptime(s, format)
}
/// Formats the time according to the format string.
pub fn strftime(format: &str, tm: &Tm) -> ~str {
// unsafe only because do_strftime is annoying to make pure
// (it does IO with a str_reader)
unsafe { do_strftime(format, tm) }
do_strftime(format, tm)
}
pub impl Tm {

View file

@ -252,7 +252,6 @@ pub impl FileMap {
// get a line from the list of pre-computed line-beginnings
pub fn get_line(&self, line: int) -> ~str {
unsafe {
let begin: BytePos = self.lines[line] - self.start_pos;
let begin = begin.to_uint();
let end = match str::find_char_from(*self.src, '\n', begin) {
@ -261,7 +260,6 @@ pub impl FileMap {
};
str::slice(*self.src, begin, end).to_owned()
}
}
pub fn record_multibyte_char(&self, pos: BytePos, bytes: uint) {
assert!(bytes >=2 && bytes <= 4);

View file

@ -175,12 +175,10 @@ fn byte_offset(rdr: @mut StringReader) -> BytePos {
}
pub fn get_str_from(rdr: @mut StringReader, start: BytePos) -> ~str {
unsafe {
// I'm pretty skeptical about this subtraction. What if there's a
// multi-byte character before the mark?
return str::slice(*rdr.src, start.to_uint() - 1u,
byte_offset(rdr).to_uint() - 1u).to_owned();
}
}
// EFFECT: advance the StringReader by one character. If a newline is