diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 35744f3f16b39..21b5557db99f2 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -188,6 +188,34 @@ use ptr; /// A mutable memory location. /// +/// # Examples +/// +/// Here you can see how using `Cell` allows to use mutable field inside +/// immutable struct (which is also called 'interior mutability'). +/// +/// ``` +/// use std::cell::Cell; +/// +/// struct SomeStruct { +/// regular_field: u8, +/// special_field: Cell, +/// } +/// +/// let my_struct = SomeStruct { +/// regular_field: 0, +/// special_field: Cell::new(1), +/// }; +/// +/// let new_value = 100; +/// +/// // ERROR, because my_struct is immutable +/// // my_struct.regular_field = new_value; +/// +/// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell +/// my_struct.special_field.set(new_value); +/// assert_eq!(my_struct.special_field.get(), new_value); +/// ``` +/// /// See the [module-level documentation](index.html) for more. #[stable(feature = "rust1", since = "1.0.0")] pub struct Cell { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index e0b0aca1261bf..f245b1503dab8 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -174,7 +174,7 @@ pub struct AssociatedItem { pub method_has_self_argument: bool, } -#[derive(Copy, Clone, PartialEq, Eq, Debug, RustcEncodable, RustcDecodable)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, RustcEncodable, RustcDecodable)] pub enum AssociatedKind { Const, Method, diff --git a/src/librustc_data_structures/bitslice.rs b/src/librustc_data_structures/bitslice.rs index ba53578e57918..f74af6ee1632e 100644 --- a/src/librustc_data_structures/bitslice.rs +++ b/src/librustc_data_structures/bitslice.rs @@ -134,9 +134,11 @@ pub trait BitwiseOperator { pub struct Union; impl BitwiseOperator for Union { + #[inline] fn join(&self, a: usize, b: usize) -> usize { a | b } } pub struct Subtract; impl BitwiseOperator for Subtract { + #[inline] fn join(&self, a: usize, b: usize) -> usize { a & !b } } diff --git a/src/librustc_llvm/archive_ro.rs b/src/librustc_llvm/archive_ro.rs index b3f5f8e536052..0b24e55541b07 100644 --- a/src/librustc_llvm/archive_ro.rs +++ b/src/librustc_llvm/archive_ro.rs @@ -39,14 +39,14 @@ impl ArchiveRO { /// /// If this archive is used with a mutable method, then an error will be /// raised. - pub fn open(dst: &Path) -> Option { + pub fn open(dst: &Path) -> Result { return unsafe { let s = path2cstr(dst); let ar = ::LLVMRustOpenArchive(s.as_ptr()); if ar.is_null() { - None + Err(::last_error().unwrap_or("failed to open archive".to_string())) } else { - Some(ArchiveRO { ptr: ar }) + Ok(ArchiveRO { ptr: ar }) } }; diff --git a/src/librustc_trans/back/archive.rs b/src/librustc_trans/back/archive.rs index 902065c8688d0..6ec40bd689c23 100644 --- a/src/librustc_trans/back/archive.rs +++ b/src/librustc_trans/back/archive.rs @@ -126,7 +126,7 @@ impl<'a> ArchiveBuilder<'a> { Some(ref src) => src, None => return None, }; - self.src_archive = Some(ArchiveRO::open(src)); + self.src_archive = Some(ArchiveRO::open(src).ok()); self.src_archive.as_ref().unwrap().as_ref() } @@ -186,9 +186,8 @@ impl<'a> ArchiveBuilder<'a> { where F: FnMut(&str) -> bool + 'static { let archive = match ArchiveRO::open(archive) { - Some(ar) => ar, - None => return Err(io::Error::new(io::ErrorKind::Other, - "failed to open archive")), + Ok(ar) => ar, + Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)), }; self.additions.push(Addition::Archive { archive: archive, diff --git a/src/librustc_trans/metadata.rs b/src/librustc_trans/metadata.rs index 2c0148dfbb371..883808c59091a 100644 --- a/src/librustc_trans/metadata.rs +++ b/src/librustc_trans/metadata.rs @@ -31,10 +31,10 @@ impl MetadataLoader for LlvmMetadataLoader { // just keeping the archive along while the metadata is in use. let archive = ArchiveRO::open(filename) .map(|ar| OwningRef::new(box ar)) - .ok_or_else(|| { - debug!("llvm didn't like `{}`", filename.display()); - format!("failed to read rlib metadata: '{}'", filename.display()) - })?; + .map_err(|e| { + debug!("llvm didn't like `{}`: {}", filename.display(), e); + format!("failed to read rlib metadata in '{}': {}", filename.display(), e) + })?; let buf: OwningRef<_, [u8]> = archive .try_map(|ar| { ar.iter() @@ -42,10 +42,10 @@ impl MetadataLoader for LlvmMetadataLoader { .find(|sect| sect.name() == Some(METADATA_FILENAME)) .map(|s| s.data()) .ok_or_else(|| { - debug!("didn't find '{}' in the archive", METADATA_FILENAME); - format!("failed to read rlib metadata: '{}'", - filename.display()) - }) + debug!("didn't find '{}' in the archive", METADATA_FILENAME); + format!("failed to read rlib metadata: '{}'", + filename.display()) + }) })?; Ok(buf.erase_owner()) } diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index d80df5f18b610..80a223c7d74ea 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -123,13 +123,13 @@ pub struct HashSet { } impl HashSet { - /// Creates an empty HashSet. + /// Creates an empty `HashSet`. /// /// # Examples /// /// ``` /// use std::collections::HashSet; - /// let mut set: HashSet = HashSet::new(); + /// let set: HashSet = HashSet::new(); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] @@ -146,7 +146,8 @@ impl HashSet { /// /// ``` /// use std::collections::HashSet; - /// let mut set: HashSet = HashSet::with_capacity(10); + /// let set: HashSet = HashSet::with_capacity(10); + /// assert!(set.capacity() >= 10); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] @@ -215,6 +216,17 @@ impl HashSet /// Returns a reference to the set's [`BuildHasher`]. /// /// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// use std::collections::hash_map::RandomState; + /// + /// let hasher = RandomState::new(); + /// let set: HashSet = HashSet::with_hasher(hasher); + /// let hasher: &RandomState = set.hasher(); + /// ``` #[stable(feature = "hashmap_public_hasher", since = "1.9.0")] pub fn hasher(&self) -> &S { self.map.hasher() @@ -249,6 +261,7 @@ impl HashSet /// use std::collections::HashSet; /// let mut set: HashSet = HashSet::new(); /// set.reserve(10); + /// assert!(set.capacity() >= 10); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve(&mut self, additional: usize) { @@ -312,13 +325,13 @@ impl HashSet /// println!("{}", x); // Print 1 /// } /// - /// let diff: HashSet<_> = a.difference(&b).cloned().collect(); - /// assert_eq!(diff, [1].iter().cloned().collect()); + /// let diff: HashSet<_> = a.difference(&b).collect(); + /// assert_eq!(diff, [1].iter().collect()); /// /// // Note that difference is not symmetric, /// // and `b - a` means something else: - /// let diff: HashSet<_> = b.difference(&a).cloned().collect(); - /// assert_eq!(diff, [4].iter().cloned().collect()); + /// let diff: HashSet<_> = b.difference(&a).collect(); + /// assert_eq!(diff, [4].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn difference<'a>(&'a self, other: &'a HashSet) -> Difference<'a, T, S> { @@ -343,11 +356,11 @@ impl HashSet /// println!("{}", x); /// } /// - /// let diff1: HashSet<_> = a.symmetric_difference(&b).cloned().collect(); - /// let diff2: HashSet<_> = b.symmetric_difference(&a).cloned().collect(); + /// let diff1: HashSet<_> = a.symmetric_difference(&b).collect(); + /// let diff2: HashSet<_> = b.symmetric_difference(&a).collect(); /// /// assert_eq!(diff1, diff2); - /// assert_eq!(diff1, [1, 4].iter().cloned().collect()); + /// assert_eq!(diff1, [1, 4].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn symmetric_difference<'a>(&'a self, @@ -371,8 +384,8 @@ impl HashSet /// println!("{}", x); /// } /// - /// let intersection: HashSet<_> = a.intersection(&b).cloned().collect(); - /// assert_eq!(intersection, [2, 3].iter().cloned().collect()); + /// let intersection: HashSet<_> = a.intersection(&b).collect(); + /// assert_eq!(intersection, [2, 3].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn intersection<'a>(&'a self, other: &'a HashSet) -> Intersection<'a, T, S> { @@ -397,8 +410,8 @@ impl HashSet /// println!("{}", x); /// } /// - /// let union: HashSet<_> = a.union(&b).cloned().collect(); - /// assert_eq!(union, [1, 2, 3, 4].iter().cloned().collect()); + /// let union: HashSet<_> = a.union(&b).collect(); + /// assert_eq!(union, [1, 2, 3, 4].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn union<'a>(&'a self, other: &'a HashSet) -> Union<'a, T, S> { @@ -440,6 +453,22 @@ impl HashSet } /// Clears the set, returning all elements in an iterator. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// + /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// assert!(!set.is_empty()); + /// + /// // print 1, 2, 3 in an arbitrary order + /// for i in set.drain() { + /// println!("{}", i); + /// } + /// + /// assert!(set.is_empty()); + /// ``` #[inline] #[stable(feature = "drain", since = "1.6.0")] pub fn drain(&mut self) -> Drain { diff --git a/src/libstd/os/solaris/raw.rs b/src/libstd/os/solaris/raw.rs index b84fdba9ca25c..5a813c5c76bca 100644 --- a/src/libstd/os/solaris/raw.rs +++ b/src/libstd/os/solaris/raw.rs @@ -32,7 +32,7 @@ use os::unix::raw::{uid_t, gid_t}; #[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = i64; #[stable(feature = "pthread_t", since = "1.8.0")] -pub type pthread_t = usize; +pub type pthread_t = u32; #[repr(C)] #[derive(Clone)]