From 4f61e160326676cdcce94b9d5bca7d88c5f40ee9 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Sun, 8 Feb 2015 00:17:04 +0100 Subject: Fix std::ops::Range size_hint and ExactSizeIterator impls When self.start > self.end, these iterators simply return None, so we adjust the size_hint to just return zero in this case. Certain optimizations can be implemented in and outside libstd if we know we can trust the size_hint for all inputs to for example Range. This corrects the ExactSizeIterator implementations, which IMO were unsound and incorrect previously, since they allowed a range like (2..1) to return a size_hint of -1us in when debug assertions are turned off. --- src/libcoretest/iter.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/libcoretest') diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs index c9cdf50fdbd..3f8e330b332 100644 --- a/src/libcoretest/iter.rs +++ b/src/libcoretest/iter.rs @@ -756,6 +756,7 @@ fn test_range() { // this test is only meaningful when sizeof uint < sizeof u64 assert_eq!((uint::MAX - 1..uint::MAX).size_hint(), (1, Some(1))); assert_eq!((-10..-1).size_hint(), (9, Some(9))); + assert_eq!((-1..-10).size_hint(), (0, Some(0))); } #[test] -- cgit 1.4.1-3-g733a5 From 792dc8d067d1f11e08e859ccdd45d59436773fc9 Mon Sep 17 00:00:00 2001 From: Marvin Löbel Date: Tue, 10 Feb 2015 14:37:44 +0100 Subject: Made the `ptr::Unique` type accept unsized types, to allow for use cases like sending a raw pointer slice across thread boundaries. --- src/libcore/ptr.rs | 6 +++--- src/libcoretest/ptr.rs | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'src/libcoretest') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index ba1eae551ff..bf801a88ca5 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -522,21 +522,21 @@ impl PartialOrd for *mut T { /// Useful for building abstractions like `Vec` or `Box`, which /// internally use raw pointers to manage the memory that they own. #[unstable(feature = "core", reason = "recently added to this module")] -pub struct Unique(pub *mut T); +pub struct Unique(pub *mut T); /// `Unique` pointers are `Send` if `T` is `Send` because the data they /// reference is unaliased. Note that this aliasing invariant is /// unenforced by the type system; the abstraction using the /// `Unique` must enforce it. #[unstable(feature = "core", reason = "recently added to this module")] -unsafe impl Send for Unique { } +unsafe impl Send for Unique { } /// `Unique` pointers are `Sync` if `T` is `Sync` because the data they /// reference is unaliased. Note that this aliasing invariant is /// unenforced by the type system; the abstraction using the /// `Unique` must enforce it. #[unstable(feature = "core", reason = "recently added to this module")] -unsafe impl Sync for Unique { } +unsafe impl Sync for Unique { } impl Unique { /// Returns a null Unique. diff --git a/src/libcoretest/ptr.rs b/src/libcoretest/ptr.rs index 7f0b97c53d4..2365b907b3f 100644 --- a/src/libcoretest/ptr.rs +++ b/src/libcoretest/ptr.rs @@ -167,3 +167,12 @@ fn test_set_memory() { unsafe { set_memory(ptr, 5u8, xs.len()); } assert!(xs == [5u8; 20]); } + +#[test] +fn test_unsized_unique() { + let xs: &mut [_] = &mut [1, 2, 3]; + let ptr = Unique(xs as *mut [_]); + let ys = unsafe { &mut *ptr.0 }; + let zs: &mut [_] = &mut [1, 2, 3]; + assert!(ys == zs); +} -- cgit 1.4.1-3-g733a5