Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@
//! the optional owned box, `Option<Box<T>>`.
//!
//! The following example uses `Option` to create an optional box of
//! `int`. Notice that in order to use the inner `int` value first the
//! `i32`. Notice that in order to use the inner `i32` value first the
//! `check_optional` function needs to use pattern matching to
//! determine whether the box has a value (i.e. it is `Some(...)`) or
//! not (`None`).
//!
//! ```
//! let optional: Option<Box<int>> = None;
//! let optional: Option<Box<i32>> = None;
//! check_optional(&optional);
//!
//! let optional: Option<Box<int>> = Some(Box::new(9000));
//! let optional: Option<Box<i32>> = Some(Box::new(9000));
//! check_optional(&optional);
//!
//! fn check_optional(optional: &Option<Box<int>>) {
//! fn check_optional(optional: &Option<Box<i32>>) {
//! match *optional {
//! Some(ref p) => println!("have value {}", p),
//! None => println!("have no value")
Expand Down Expand Up @@ -108,7 +108,7 @@
//! Initialize a result to `None` before a loop:
//!
//! ```
//! enum Kingdom { Plant(uint, &'static str), Animal(uint, &'static str) }
//! enum Kingdom { Plant(usize, &'static str), Animal(usize, &'static str) }
//!
//! // A list of data to search through.
//! let all_the_big_things = [
Expand Down Expand Up @@ -188,10 +188,10 @@ impl<T> Option<T> {
/// # Example
///
/// ```
/// let x: Option<uint> = Some(2);
/// let x: Option<usize> = Some(2);
/// assert_eq!(x.is_some(), true);
///
/// let x: Option<uint> = None;
/// let x: Option<usize> = None;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to leave these as usize rather than i32? (for pedagogy against using usize randomly)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these can actually be inferred if we want i32, although I dunno if that would "mess up" the quality of the docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured the explicit types were there for a reason (in particular, type inference is hard on the reader, and these docs are largely targeting total newbies). In that case, I don't know that it matters what type is there. But I guess I can change it to u32 or i32.

/// assert_eq!(x.is_some(), false);
/// ```
#[inline]
Expand All @@ -208,10 +208,10 @@ impl<T> Option<T> {
/// # Example
///
/// ```
/// let x: Option<uint> = Some(2);
/// let x: Option<usize> = Some(2);
/// assert_eq!(x.is_none(), false);
///
/// let x: Option<uint> = None;
/// let x: Option<usize> = None;
/// assert_eq!(x.is_none(), true);
/// ```
#[inline]
Expand All @@ -228,7 +228,7 @@ impl<T> Option<T> {
///
/// # Example
///
/// Convert an `Option<String>` into an `Option<int>`, preserving the original.
/// Convert an `Option<String>` into an `Option<usize>`, preserving the original.
/// The `map` method takes the `self` argument by value, consuming the original,
/// so this technique uses `as_ref` to first take an `Option` to a reference
/// to the value inside the original.
Expand All @@ -237,7 +237,7 @@ impl<T> Option<T> {
/// let num_as_str: Option<String> = Some("10".to_string());
/// // First, cast `Option<String>` to `Option<&String>` with `as_ref`,
/// // then consume *that* with `map`, leaving `num_as_str` on the stack.
/// let num_as_int: Option<uint> = num_as_str.as_ref().map(|n| n.len());
/// let num_as_int: Option<usize> = num_as_str.as_ref().map(|n| n.len());
/// println!("still can print num_as_str: {:?}", num_as_str);
/// ```
#[inline]
Expand Down Expand Up @@ -406,12 +406,12 @@ impl<T> Option<T> {
///
/// # Example
///
/// Convert an `Option<String>` into an `Option<uint>`, consuming the original:
/// Convert an `Option<String>` into an `Option<usize>`, consuming the original:
///
/// ```
/// let num_as_str: Option<String> = Some("10".to_string());
/// // `Option::map` takes self *by value*, consuming `num_as_str`
/// let num_as_int: Option<uint> = num_as_str.map(|n| n.len());
/// let num_as_int: Option<usize> = num_as_str.map(|n| n.len());
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -518,7 +518,7 @@ impl<T> Option<T> {
/// let x = Some(4);
/// assert_eq!(x.iter().next(), Some(&4));
///
/// let x: Option<uint> = None;
/// let x: Option<usize> = None;
/// assert_eq!(x.iter().next(), None);
/// ```
#[inline]
Expand All @@ -539,7 +539,7 @@ impl<T> Option<T> {
/// }
/// assert_eq!(x, Some(42));
///
/// let mut x: Option<uint> = None;
/// let mut x: Option<usize> = None;
/// assert_eq!(x.iter_mut().next(), None);
/// ```
#[inline]
Expand Down Expand Up @@ -581,15 +581,15 @@ impl<T> Option<T> {
/// let y: Option<&str> = None;
/// assert_eq!(x.and(y), None);
///
/// let x: Option<uint> = None;
/// let x: Option<usize> = None;
/// let y = Some("foo");
/// assert_eq!(x.and(y), None);
///
/// let x = Some(2);
/// let y = Some("foo");
/// assert_eq!(x.and(y), Some("foo"));
///
/// let x: Option<uint> = None;
/// let x: Option<usize> = None;
/// let y: Option<&str> = None;
/// assert_eq!(x.and(y), None);
/// ```
Expand All @@ -608,8 +608,8 @@ impl<T> Option<T> {
/// # Example
///
/// ```
/// fn sq(x: uint) -> Option<uint> { Some(x * x) }
/// fn nope(_: uint) -> Option<uint> { None }
/// fn sq(x: usize) -> Option<usize> { Some(x * x) }
/// fn nope(_: usize) -> Option<usize> { None }
///
/// assert_eq!(Some(2).and_then(sq).and_then(sq), Some(16));
/// assert_eq!(Some(2).and_then(sq).and_then(nope), None);
Expand Down Expand Up @@ -642,7 +642,7 @@ impl<T> Option<T> {
/// let y = Some(100);
/// assert_eq!(x.or(y), Some(2));
///
/// let x: Option<uint> = None;
/// let x: Option<usize> = None;
/// let y = None;
/// assert_eq!(x.or(y), None);
/// ```
Expand Down Expand Up @@ -690,7 +690,7 @@ impl<T> Option<T> {
/// x.take();
/// assert_eq!(x, None);
///
/// let mut x: Option<uint> = None;
/// let mut x: Option<usize> = None;
/// x.take();
/// assert_eq!(x, None);
/// ```
Expand Down Expand Up @@ -789,7 +789,7 @@ impl<A> Iterator for Item<A> {
}

#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
match self.opt {
Some(_) => (1, Some(1)),
None => (0, Some(0)),
Expand Down Expand Up @@ -817,7 +817,7 @@ impl<'a, A> Iterator for Iter<'a, A> {
#[inline]
fn next(&mut self) -> Option<&'a A> { self.inner.next() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -847,7 +847,7 @@ impl<'a, A> Iterator for IterMut<'a, A> {
#[inline]
fn next(&mut self) -> Option<&'a mut A> { self.inner.next() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -870,7 +870,7 @@ impl<A> Iterator for IntoIter<A> {
#[inline]
fn next(&mut self) -> Option<A> { self.inner.next() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -896,11 +896,11 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
/// checking for overflow:
///
/// ```rust
/// use std::uint;
/// use std::u16;
///
/// let v = vec!(1, 2);
/// let res: Option<Vec<uint>> = v.iter().map(|&x: &uint|
/// if x == uint::MAX { None }
/// let res: Option<Vec<u16>> = v.iter().map(|&x: &u16|
/// if x == u16::MAX { None }
/// else { Some(x + 1) }
/// ).collect();
/// assert!(res == Some(vec!(2, 3)));
Expand Down
40 changes: 20 additions & 20 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
//! ## 1. Coerce a reference (`&T`) or mutable reference (`&mut T`).
//!
//! ```
//! let my_num: int = 10;
//! let my_num_ptr: *const int = &my_num;
//! let mut my_speed: int = 88;
//! let my_speed_ptr: *mut int = &mut my_speed;
//! let my_num: i32 = 10;
//! let my_num_ptr: *const i32 = &my_num;
//! let mut my_speed: i32 = 88;
//! let my_speed_ptr: *mut i32 = &mut my_speed;
//! ```
//!
//! This does not take ownership of the original allocation
Expand All @@ -49,15 +49,15 @@
//! use std::mem;
//!
//! unsafe {
//! let my_num: Box<int> = Box::new(10);
//! let my_num: *const int = mem::transmute(my_num);
//! let my_speed: Box<int> = Box::new(88);
//! let my_speed: *mut int = mem::transmute(my_speed);
//! let my_num: Box<i32> = Box::new(10);
//! let my_num: *const i32 = mem::transmute(my_num);
//! let my_speed: Box<i32> = Box::new(88);
//! let my_speed: *mut i32 = mem::transmute(my_speed);
//!
//! // By taking ownership of the original `Box<T>` though
//! // we are obligated to transmute it back later to be destroyed.
//! drop(mem::transmute::<_, Box<int>>(my_speed));
//! drop(mem::transmute::<_, Box<int>>(my_num));
//! drop(mem::transmute::<_, Box<i32>>(my_speed));
//! drop(mem::transmute::<_, Box<i32>>(my_num));
//! }
//! ```
//!
Expand All @@ -73,7 +73,7 @@
//!
//! fn main() {
//! unsafe {
//! let my_num: *mut int = libc::malloc(mem::size_of::<int>() as libc::size_t) as *mut int;
//! let my_num: *mut i32 = libc::malloc(mem::size_of::<i32>() as libc::size_t) as *mut i32;
//! if my_num.is_null() {
//! panic!("failed to allocate memory");
//! }
Expand Down Expand Up @@ -117,7 +117,7 @@ pub use intrinsics::set_memory;
/// ```
/// use std::ptr;
///
/// let p: *const int = ptr::null();
/// let p: *const i32 = ptr::null();
/// assert!(p.is_null());
/// ```
#[inline]
Expand All @@ -131,7 +131,7 @@ pub fn null<T>() -> *const T { 0 as *const T }
/// ```
/// use std::ptr;
///
/// let p: *mut int = ptr::null_mut();
/// let p: *mut i32 = ptr::null_mut();
/// assert!(p.is_null());
/// ```
#[inline]
Expand All @@ -148,7 +148,7 @@ pub fn null_mut<T>() -> *mut T { 0 as *mut T }
#[inline]
#[unstable(feature = "core",
reason = "may play a larger role in std::ptr future extensions")]
pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
pub unsafe fn zero_memory<T>(dst: *mut T, count: usize) {
set_memory(dst, 0, count);
}

Expand Down Expand Up @@ -276,7 +276,7 @@ pub trait PtrExt: Sized {
/// Otherwise `offset` invokes Undefined Behaviour, regardless of whether
/// the pointer is used.
#[stable(feature = "rust1", since = "1.0.0")]
unsafe fn offset(self, count: int) -> Self;
unsafe fn offset(self, count: isize) -> Self;
}

/// Methods on mutable raw pointers
Expand All @@ -303,11 +303,11 @@ impl<T> PtrExt for *const T {

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn is_null(self) -> bool { self as uint == 0 }
fn is_null(self) -> bool { self as usize == 0 }

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
unsafe fn offset(self, count: int) -> *const T {
unsafe fn offset(self, count: isize) -> *const T {
intrinsics::offset(self, count)
}

Expand All @@ -330,11 +330,11 @@ impl<T> PtrExt for *mut T {

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn is_null(self) -> bool { self as uint == 0 }
fn is_null(self) -> bool { self as usize == 0 }

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
unsafe fn offset(self, count: int) -> *mut T {
unsafe fn offset(self, count: isize) -> *mut T {
intrinsics::offset(self, count) as *mut T
}

Expand Down Expand Up @@ -553,7 +553,7 @@ impl<T> Unique<T> {
/// Return an (unsafe) pointer into the memory owned by `self`.
#[unstable(feature = "core",
reason = "recently added to this module")]
pub unsafe fn offset(self, offset: int) -> *mut T {
pub unsafe fn offset(self, offset: isize) -> *mut T {
self.ptr.offset(offset)
}
}
Expand Down