From 5b6d0245b8055f176c606604ac6cc0f50ef79b45 Mon Sep 17 00:00:00 2001 From: Tristan Storch Date: Tue, 13 Jan 2015 17:45:04 +0100 Subject: Small Readability Update --- src/libcore/str/mod.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 94ee9b7dcf6..cf8a96f4396 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -678,18 +678,15 @@ struct TwoWaySearcher { */ impl TwoWaySearcher { fn new(needle: &[u8]) -> TwoWaySearcher { - let (crit_pos1, period1) = TwoWaySearcher::maximal_suffix(needle, false); - let (crit_pos2, period2) = TwoWaySearcher::maximal_suffix(needle, true); - - let crit_pos; - let period; - if crit_pos1 > crit_pos2 { - crit_pos = crit_pos1; - period = period1; - } else { - crit_pos = crit_pos2; - period = period2; - } + let (crit_pos_false, period_false) = TwoWaySearcher::maximal_suffix(needle, false); + let (crit_pos_true, period_true) = TwoWaySearcher::maximal_suffix(needle, true); + + let (crit_pos, period) = + if crit_pos_false > crit_pos_true { + (crit_pos_false, period_false) + } else { + (crit_pos_true, period_true) + }; // This isn't in the original algorithm, as far as I'm aware. let byteset = needle.iter() -- cgit 1.4.1-3-g733a5 From 812ce6c190d896cf1cc1bef9f22c00266e962c43 Mon Sep 17 00:00:00 2001 From: we Date: Sat, 17 Jan 2015 07:34:10 +0300 Subject: Remove unnecessary explicit conversions to *const T --- src/doc/trpl/unsafe.md | 2 +- src/liballoc/heap.rs | 2 +- src/libcollections/btree/node.rs | 6 +++--- src/libcollections/ring_buf.rs | 6 +++--- src/libcollections/slice.rs | 2 +- src/libcollections/vec.rs | 12 ++++++------ src/libcore/atomic.rs | 12 ++++++------ src/libcore/ptr.rs | 2 +- src/libcore/slice.rs | 4 ++-- src/librustc_trans/trans/builder.rs | 2 +- src/librustdoc/flock.rs | 4 ++-- src/libstd/collections/hash/table.rs | 23 ++++++++++------------- src/libstd/sys/unix/backtrace.rs | 2 +- src/libstd/thread_local/mod.rs | 2 +- 14 files changed, 39 insertions(+), 42 deletions(-) (limited to 'src/libcore') diff --git a/src/doc/trpl/unsafe.md b/src/doc/trpl/unsafe.md index 075340660df..997dda98ef3 100644 --- a/src/doc/trpl/unsafe.md +++ b/src/doc/trpl/unsafe.md @@ -254,7 +254,7 @@ impl Drop for Unique { // Copy the object out from the pointer onto the stack, // where it is covered by normal Rust destructor semantics // and cleans itself up, if necessary - ptr::read(self.ptr as *const T); + ptr::read(self.ptr); // clean-up our allocation free(self.ptr as *mut c_void) diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs index b7bc1b47646..bd5b43b782e 100644 --- a/src/liballoc/heap.rs +++ b/src/liballoc/heap.rs @@ -298,7 +298,7 @@ mod imp { libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8 } else { let new_ptr = allocate(size, align); - ptr::copy_memory(new_ptr, ptr as *const u8, cmp::min(size, old_size)); + ptr::copy_memory(new_ptr, ptr, cmp::min(size, old_size)); deallocate(ptr, old_size, align); new_ptr } diff --git a/src/libcollections/btree/node.rs b/src/libcollections/btree/node.rs index 7d5422290e2..19c48f88caa 100644 --- a/src/libcollections/btree/node.rs +++ b/src/libcollections/btree/node.rs @@ -326,11 +326,11 @@ impl Node { pub fn as_slices<'a>(&'a self) -> (&'a [K], &'a [V]) { unsafe {( mem::transmute(raw::Slice { - data: self.keys.0 as *const K, + data: self.keys.0, len: self.len() }), mem::transmute(raw::Slice { - data: self.vals.0 as *const V, + data: self.vals.0, len: self.len() }) )} @@ -349,7 +349,7 @@ impl Node { } else { unsafe { mem::transmute(raw::Slice { - data: self.edges.0 as *const Node, + data: self.edges.0, len: self.len() + 1 }) } diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index c3d22675868..b9cb4be7c18 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -88,19 +88,19 @@ impl RingBuf { /// Turn ptr into a slice #[inline] unsafe fn buffer_as_slice(&self) -> &[T] { - mem::transmute(RawSlice { data: self.ptr as *const T, len: self.cap }) + mem::transmute(RawSlice { data: self.ptr, len: self.cap }) } /// Turn ptr into a mut slice #[inline] unsafe fn buffer_as_mut_slice(&mut self) -> &mut [T] { - mem::transmute(RawSlice { data: self.ptr as *const T, len: self.cap }) + mem::transmute(RawSlice { data: self.ptr, len: self.cap }) } /// Moves an element out of the buffer #[inline] unsafe fn buffer_read(&mut self, off: uint) -> T { - ptr::read(self.ptr.offset(off as int) as *const T) + ptr::read(self.ptr.offset(off as int)) } /// Writes an element into the buffer, moving it. diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 4812ecc2c0b..988ec4c661f 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -1222,7 +1222,7 @@ fn insertion_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> O &*buf_v.offset(j), (i - j) as uint); ptr::copy_nonoverlapping_memory(buf_v.offset(j), - &tmp as *const T, + &tmp, 1); mem::forget(tmp); } diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 73afefc5a03..adc7ce8f072 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -426,7 +426,7 @@ impl Vec { pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] { unsafe { mem::transmute(RawSlice { - data: *self.ptr as *const T, + data: *self.ptr, len: self.len, }) } @@ -574,7 +574,7 @@ impl Vec { let ptr = self.as_mut_ptr().offset(index as int); // copy it out, unsafely having a copy of the value on // the stack and in the vector at the same time. - ret = ptr::read(ptr as *const T); + ret = ptr::read(ptr); // Shift everything down to fill in that spot. ptr::copy_memory(ptr, &*ptr.offset(1), len - index - 1); @@ -842,7 +842,7 @@ impl Vec { // | | // end_u end_t - let t = ptr::read(pv.start_t as *const T); + let t = ptr::read(pv.start_t); // start_u start_t // | | // +-+-+-+-+-+-+-+-+-+ @@ -1414,7 +1414,7 @@ impl AsSlice for Vec { fn as_slice<'a>(&'a self) -> &'a [T] { unsafe { mem::transmute(RawSlice { - data: *self.ptr as *const T, + data: *self.ptr, len: self.len }) } @@ -1777,11 +1777,11 @@ impl Drop for PartialVecNonZeroSized { // We have instances of `U`s and `T`s in `vec`. Destruct them. while self.start_u != self.end_u { - let _ = ptr::read(self.start_u as *const U); // Run a `U` destructor. + let _ = ptr::read(self.start_u); // Run a `U` destructor. self.start_u = self.start_u.offset(1); } while self.start_t != self.end_t { - let _ = ptr::read(self.start_t as *const T); // Run a `T` destructor. + let _ = ptr::read(self.start_t); // Run a `T` destructor. self.start_t = self.start_t.offset(1); } // After this destructor ran, the destructor of `vec` will run, diff --git a/src/libcore/atomic.rs b/src/libcore/atomic.rs index aa93d9ed837..18f7fff9053 100644 --- a/src/libcore/atomic.rs +++ b/src/libcore/atomic.rs @@ -199,7 +199,7 @@ impl AtomicBool { #[inline] #[stable] pub fn load(&self, order: Ordering) -> bool { - unsafe { atomic_load(self.v.get() as *const usize, order) > 0 } + unsafe { atomic_load(self.v.get(), order) > 0 } } /// Stores a value into the bool. @@ -438,7 +438,7 @@ impl AtomicIsize { /// ``` #[inline] pub fn load(&self, order: Ordering) -> isize { - unsafe { atomic_load(self.v.get() as *const isize, order) } + unsafe { atomic_load(self.v.get(), order) } } /// Stores a value into the isize. @@ -615,7 +615,7 @@ impl AtomicUsize { /// ``` #[inline] pub fn load(&self, order: Ordering) -> usize { - unsafe { atomic_load(self.v.get() as *const usize, order) } + unsafe { atomic_load(self.v.get(), order) } } /// Stores a value into the usize. @@ -796,7 +796,7 @@ impl AtomicPtr { #[stable] pub fn load(&self, order: Ordering) -> *mut T { unsafe { - atomic_load(self.p.get() as *const *mut T, order) as *mut T + atomic_load(self.p.get(), order) as *mut T } } @@ -1070,7 +1070,7 @@ impl AtomicInt { #[inline] pub fn load(&self, order: Ordering) -> int { - unsafe { atomic_load(self.v.get() as *const int, order) } + unsafe { atomic_load(self.v.get(), order) } } #[inline] @@ -1123,7 +1123,7 @@ impl AtomicUint { #[inline] pub fn load(&self, order: Ordering) -> uint { - unsafe { atomic_load(self.v.get() as *const uint, order) } + unsafe { atomic_load(self.v.get(), order) } } #[inline] diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index baf998d0828..0b89467d63b 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -329,7 +329,7 @@ impl PtrExt for *mut T { #[inline] #[stable] unsafe fn offset(self, count: int) -> *mut T { - intrinsics::offset(self as *const T, count) as *mut T + intrinsics::offset(self, count) as *mut T } #[inline] diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 22da168911d..50cbb7a61dc 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -741,7 +741,7 @@ macro_rules! make_slice { diff / mem::size_of::<$t>() }; unsafe { - transmute::<_, $result>(RawSlice { data: $start as *const T, len: len }) + transmute::<_, $result>(RawSlice { data: $start, len: len }) } }} } @@ -1409,7 +1409,7 @@ pub unsafe fn from_raw_buf<'a, T>(p: &'a *const T, len: uint) -> &'a [T] { #[inline] #[unstable = "should be renamed to from_raw_parts_mut"] pub unsafe fn from_raw_mut_buf<'a, T>(p: &'a *mut T, len: uint) -> &'a mut [T] { - transmute(RawSlice { data: *p as *const T, len: len }) + transmute(RawSlice { data: *p, len: len }) } // diff --git a/src/librustc_trans/trans/builder.rs b/src/librustc_trans/trans/builder.rs index 75194e3d21f..2ad42110342 100644 --- a/src/librustc_trans/trans/builder.rs +++ b/src/librustc_trans/trans/builder.rs @@ -33,7 +33,7 @@ pub struct Builder<'a, 'tcx: 'a> { // lot more efficient) than doing str::as_c_str("", ...) every time. pub fn noname() -> *const c_char { static CNULL: c_char = 0; - &CNULL as *const c_char + &CNULL } impl<'a, 'tcx> Builder<'a, 'tcx> { diff --git a/src/librustdoc/flock.rs b/src/librustdoc/flock.rs index dcc90117d26..b682723631d 100644 --- a/src/librustdoc/flock.rs +++ b/src/librustdoc/flock.rs @@ -126,7 +126,7 @@ mod imp { l_sysid: 0, }; let ret = unsafe { - libc::fcntl(fd, os::F_SETLKW, &flock as *const os::flock) + libc::fcntl(fd, os::F_SETLKW, &flock) }; if ret == -1 { unsafe { libc::close(fd); } @@ -147,7 +147,7 @@ mod imp { l_sysid: 0, }; unsafe { - libc::fcntl(self.fd, os::F_SETLK, &flock as *const os::flock); + libc::fcntl(self.fd, os::F_SETLK, &flock); libc::close(self.fd); } } diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index f28b95dbe95..d810460a7d4 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -395,9 +395,6 @@ impl> + DerefMut> FullBucket { /// This works similarly to `put`, building an `EmptyBucket` out of the /// taken bucket. pub fn take(mut self) -> (EmptyBucket, K, V) { - let key = self.raw.key as *const K; - let val = self.raw.val as *const V; - self.table.size -= 1; unsafe { @@ -408,8 +405,8 @@ impl> + DerefMut> FullBucket { idx: self.idx, table: self.table }, - ptr::read(key), - ptr::read(val) + ptr::read(self.raw.key), + ptr::read(self.raw.val) ) } } @@ -477,8 +474,8 @@ impl>> GapThenFull { pub fn shift(mut self) -> Option> { unsafe { *self.gap.raw.hash = mem::replace(&mut *self.full.raw.hash, EMPTY_BUCKET); - copy_nonoverlapping_memory(self.gap.raw.key, self.full.raw.key as *const K, 1); - copy_nonoverlapping_memory(self.gap.raw.val, self.full.raw.val as *const V, 1); + copy_nonoverlapping_memory(self.gap.raw.key, self.full.raw.key, 1); + copy_nonoverlapping_memory(self.gap.raw.val, self.full.raw.val, 1); } let FullBucket { raw: prev_raw, idx: prev_idx, .. } = self.full; @@ -781,8 +778,8 @@ impl<'a, K, V> Iterator for RevMoveBuckets<'a, K, V> { if *self.raw.hash != EMPTY_BUCKET { self.elems_left -= 1; return Some(( - ptr::read(self.raw.key as *const K), - ptr::read(self.raw.val as *const V) + ptr::read(self.raw.key), + ptr::read(self.raw.val) )); } } @@ -878,8 +875,8 @@ impl Iterator for IntoIter { SafeHash { hash: *bucket.hash, }, - ptr::read(bucket.key as *const K), - ptr::read(bucket.val as *const V) + ptr::read(bucket.key), + ptr::read(bucket.val) ) } }) @@ -906,8 +903,8 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> { SafeHash { hash: ptr::replace(bucket.hash, EMPTY_BUCKET), }, - ptr::read(bucket.key as *const K), - ptr::read(bucket.val as *const V) + ptr::read(bucket.key), + ptr::read(bucket.val) ) } }) diff --git a/src/libstd/sys/unix/backtrace.rs b/src/libstd/sys/unix/backtrace.rs index 7164931c55a..70b9c012b00 100644 --- a/src/libstd/sys/unix/backtrace.rs +++ b/src/libstd/sys/unix/backtrace.rs @@ -229,7 +229,7 @@ fn print(w: &mut Writer, idx: int, addr: *mut libc::c_void) -> IoResult<()> { } let mut info: Dl_info = unsafe { intrinsics::init() }; - if unsafe { dladdr(addr as *const libc::c_void, &mut info) == 0 } { + if unsafe { dladdr(addr, &mut info) == 0 } { output(w, idx,addr, None) } else { output(w, idx, addr, Some(unsafe { diff --git a/src/libstd/thread_local/mod.rs b/src/libstd/thread_local/mod.rs index e7c4e4ccdfb..4c99cff34da 100644 --- a/src/libstd/thread_local/mod.rs +++ b/src/libstd/thread_local/mod.rs @@ -449,7 +449,7 @@ mod imp { // destructor as running for this thread so calls to `get` will return // `None`. *(*ptr).dtor_running.get() = true; - ptr::read((*ptr).inner.get() as *const T); + ptr::read((*ptr).inner.get()); } } -- cgit 1.4.1-3-g733a5 From a674f852db184f07b6ccc81b9fab230036873e57 Mon Sep 17 00:00:00 2001 From: Diggory Blake Date: Mon, 19 Jan 2015 05:43:15 +0000 Subject: Ranges implement Clone where possible --- src/libcore/ops.rs | 8 ++++---- src/test/run-pass/issue-21384.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/test/run-pass/issue-21384.rs (limited to 'src/libcore') diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index db7177e26fa..7131253d5c4 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -880,7 +880,7 @@ pub trait IndexMut { } /// An unbounded range. -#[derive(Copy, PartialEq, Eq)] +#[derive(Copy, Clone, PartialEq, Eq)] #[lang="full_range"] #[unstable = "API still in development"] pub struct FullRange; @@ -893,7 +893,7 @@ impl fmt::Show for FullRange { } /// A (half-open) range which is bounded at both ends. -#[derive(Copy, PartialEq, Eq)] +#[derive(Copy, Clone, PartialEq, Eq)] #[lang="range"] #[unstable = "API still in development"] pub struct Range { @@ -952,7 +952,7 @@ impl fmt::Show for Range { } /// A range which is only bounded below. -#[derive(Copy, PartialEq, Eq)] +#[derive(Copy, Clone, PartialEq, Eq)] #[lang="range_from"] #[unstable = "API still in development"] pub struct RangeFrom { @@ -981,7 +981,7 @@ impl fmt::Show for RangeFrom { } /// A range which is only bounded above. -#[derive(Copy, PartialEq, Eq)] +#[derive(Copy, Clone, PartialEq, Eq)] #[lang="range_to"] #[unstable = "API still in development"] pub struct RangeTo { diff --git a/src/test/run-pass/issue-21384.rs b/src/test/run-pass/issue-21384.rs new file mode 100644 index 00000000000..0ec33d218f9 --- /dev/null +++ b/src/test/run-pass/issue-21384.rs @@ -0,0 +1,27 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn test(arg: T) -> T { + arg.clone() +} + +#[derive(PartialEq)] +struct Test(int); + +fn main() { + // Check that ranges implement clone + assert!(test(1..5) == (1..5)); + assert!(test(..5) == (..5)); + assert!(test(1..) == (1..)); + assert!(test(FullRange) == (FullRange)); + + // Check that ranges can still be used with non-clone limits + assert!((Test(1)..Test(5)) == (Test(1)..Test(5))); +} -- cgit 1.4.1-3-g733a5 From a09df2cb9d1e6025a9565f3ef7983cb743b421a3 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 19 Jan 2015 15:16:48 -0500 Subject: impl Hash for arrays closes #21402 cc #15294 --- src/libcore/array.rs | 7 +++++++ src/test/run-pass/issue-21402.rs | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/test/run-pass/issue-21402.rs (limited to 'src/libcore') diff --git a/src/libcore/array.rs b/src/libcore/array.rs index c07fac108d6..0cc31bf70de 100644 --- a/src/libcore/array.rs +++ b/src/libcore/array.rs @@ -17,6 +17,7 @@ use clone::Clone; use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; use fmt; +use hash::{Hash, Hasher, self}; use marker::Copy; use ops::{Deref, FullRange}; use option::Option; @@ -32,6 +33,12 @@ macro_rules! array_impls { } } + impl> Hash for [T; $N] { + fn hash(&self, state: &mut S) { + Hash::hash(&self[], state) + } + } + #[unstable = "waiting for Show to stabilize"] impl fmt::Show for [T; $N] { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/src/test/run-pass/issue-21402.rs b/src/test/run-pass/issue-21402.rs new file mode 100644 index 00000000000..6be7cea2928 --- /dev/null +++ b/src/test/run-pass/issue-21402.rs @@ -0,0 +1,18 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[derive(Hash)] +struct Foo { + a: Vec, + b: (bool, bool), + c: [bool; 2], +} + +fn main() {} -- cgit 1.4.1-3-g733a5