Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename .as_mut() to .as_mut_ref() in Option/Result/AnyMutRefExt #14299

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl<T> Deque<T> for DList<T> {
/// Provide a mutable reference to the front element, or None if the list is empty
#[inline]
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
self.list_head.as_mut().map(|head| &mut head.value)
self.list_head.as_mut_ref().map(|head| &mut head.value)
}

/// Provide a reference to the back element, or None if the list is empty
Expand Down
35 changes: 21 additions & 14 deletions src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
//! This module implements the `Any` trait, which enables dynamic typing
//! of any type, through runtime reflection.
//!
//! `Any` itself can be used to get a `TypeId`, and has more features when used as a trait object.
//! As `&Any` (a borrowed trait object), it has the `is` and `as_ref` methods, to test if the
//! contained value is of a given type, and to get a reference to the inner value as a type. As
//! `&mut Any`, there is also the `as_mut` method, for getting a mutable reference to the inner
//! value. `Box<Any>` adds the `move` method, which will unwrap a `Box<T>` from the object. See
//! the extension traits (`*Ext`) for the full details.
//! `Any` itself can be used to get a `TypeId`, and has more features when used
//! as a trait object. As `&Any` (a borrowed trait object), it has the `is` and
//! `as_ref` methods, to test if the contained value is of a given type,
//! and to get a reference to the inner value as a type. As `&mut Any`, there
//! is also the `as_mut_ref` method, for getting a mutable reference to the
//! inner value. `Box<Any>` adds the `move` method, which will unwrap a
//! `Box<T>` from the object. See the extension traits (`*Ext`) for the full
//! details.

use mem::{transmute, transmute_copy};
use option::{Option, Some, None};
Expand Down Expand Up @@ -94,12 +96,17 @@ impl<'a> AnyRefExt<'a> for &'a Any {
pub trait AnyMutRefExt<'a> {
/// Returns some mutable reference to the boxed value if it is of type `T`, or
/// `None` if it isn't.
fn as_mut<T: 'static>(self) -> Option<&'a mut T>;
fn as_mut_ref<T: 'static>(self) -> Option<&'a mut T>;

/// Deprecated name for `as_mut_ref()`.
#[deprecated = "replaced by .as_mut_ref()"]
#[inline]
fn as_mut<T: 'static>(self) -> Option<&'a mut T> { self.as_mut_ref() }
}

impl<'a> AnyMutRefExt<'a> for &'a mut Any {
#[inline]
fn as_mut<T: 'static>(self) -> Option<&'a mut T> {
fn as_mut_ref<T: 'static>(self) -> Option<&'a mut T> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
Expand Down Expand Up @@ -184,38 +191,38 @@ mod tests {
let tmp: &mut uint = b;
let b_r = tmp as &mut Any;

match a_r.as_mut::<uint>() {
match a_r.as_mut_ref::<uint>() {
Some(x) => {
assert_eq!(*x, 5u);
*x = 612;
}
x => fail!("Unexpected value {}", x)
}

match b_r.as_mut::<uint>() {
match b_r.as_mut_ref::<uint>() {
Some(x) => {
assert_eq!(*x, 7u);
*x = 413;
}
x => fail!("Unexpected value {}", x)
}

match a_r.as_mut::<Test>() {
match a_r.as_mut_ref::<Test>() {
None => (),
x => fail!("Unexpected value {}", x)
}

match b_r.as_mut::<Test>() {
match b_r.as_mut_ref::<Test>() {
None => (),
x => fail!("Unexpected value {}", x)
}

match a_r.as_mut::<uint>() {
match a_r.as_mut_ref::<uint>() {
Some(&612) => {}
x => fail!("Unexpected value {}", x)
}

match b_r.as_mut::<uint>() {
match b_r.as_mut_ref::<uint>() {
Some(&413) => {}
x => fail!("Unexpected value {}", x)
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,7 @@ impl<'a, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for FlatMap<'a, A, T,
}
}
match self.iter.next().map(|x| (self.f)(x)) {
None => return self.backiter.as_mut().and_then(|it| it.next()),
None => return self.backiter.as_mut_ref().and_then(|it| it.next()),
next => self.frontiter = next,
}
}
Expand Down Expand Up @@ -1735,7 +1735,7 @@ impl<'a,
}
}
match self.iter.next_back().map(|x| (self.f)(x)) {
None => return self.frontiter.as_mut().and_then(|it| it.next_back()),
None => return self.frontiter.as_mut_ref().and_then(|it| it.next_back()),
next => self.backiter = next,
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,17 @@ impl<T> Option<T> {

/// Convert from `Option<T>` to `Option<&mut T>`
#[inline]
pub fn as_mut<'r>(&'r mut self) -> Option<&'r mut T> {
pub fn as_mut_ref<'r>(&'r mut self) -> Option<&'r mut T> {
match *self { Some(ref mut x) => Some(x), None => None }
}

/// Deprecated name for `as_mut_ref()`.
#[deprecated = "replaced by .as_mut_ref()"]
#[inline]
pub fn as_mut<'r>(&'r mut self) -> Option<&'r mut T> {
self.as_mut_ref()
}

/// Convert from `Option<T>` to `&[T]` (without copying)
#[inline]
pub fn as_slice<'r>(&'r self) -> &'r [T] {
Expand Down Expand Up @@ -330,7 +337,7 @@ impl<T> Option<T> {
/// Returns a mutable iterator over the possibly contained value.
#[inline]
pub fn mut_iter<'r>(&'r mut self) -> Item<&'r mut T> {
Item{opt: self.as_mut()}
Item{opt: self.as_mut_ref()}
}

/// Returns a consuming iterator over the possibly contained value.
Expand Down
9 changes: 8 additions & 1 deletion src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,20 @@ impl<T, E> Result<T, E> {

/// Convert from `Result<T, E>` to `Result<&mut T, &mut E>`
#[inline]
pub fn as_mut<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E> {
pub fn as_mut_ref<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E> {
match *self {
Ok(ref mut x) => Ok(x),
Err(ref mut x) => Err(x),
}
}

/// Deprecated name for `as_mut_ref()`.
#[deprecated = "replaced by .as_mut_ref()"]
#[inline]
pub fn as_mut<'r>(&'r mut self) -> Result<&'r mut T, &'r mut E> {
self.as_mut_ref()
}

/////////////////////////////////////////////////////////////////////////
// Transforming contained values
/////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sync/spsc_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl<T: Send> Queue<T> {
let tail = self.tail;
let next = (*tail).next.load(Acquire);
if next.is_null() { return None }
return (*next).value.as_mut();
return (*next).value.as_mut_ref();
}
}
}
Expand Down