Skip to content

Commit

Permalink
Stabilize std::convert and related code
Browse files Browse the repository at this point in the history
* Marks `#[stable]` the contents of the `std::convert` module.

* Added methods `PathBuf::as_path`, `OsString::as_os_str`,
  `String::as_str`, `Vec::{as_slice, as_mut_slice}`.

* Deprecates `OsStr::from_str` in favor of a new, stable, and more
  general `OsStr::new`.

* Adds unstable methods `OsString::from_bytes` and `OsStr::{to_bytes,
  to_cstring}` for ergonomic FFI usage.

[breaking-change]
  • Loading branch information
aturon committed Mar 31, 2015
1 parent 6cf3b0b commit 9fc51ef
Show file tree
Hide file tree
Showing 25 changed files with 132 additions and 70 deletions.
1 change: 0 additions & 1 deletion src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#![feature(std_misc)]
#![feature(test)]
#![feature(path_ext)]
#![feature(convert)]
#![feature(str_char)]

#![deny(warnings)]
Expand Down
1 change: 0 additions & 1 deletion src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#![feature(unsafe_no_drop_flag, filling_drop)]
#![feature(step_by)]
#![feature(str_char)]
#![feature(convert)]
#![feature(slice_patterns)]
#![feature(debug_builders)]
#![cfg_attr(test, feature(rand, rustc_private, test, hash, collections))]
Expand Down
9 changes: 8 additions & 1 deletion src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ impl String {
self.vec
}

/// Extract a string slice containing the entire string.
#[inline]
#[unstable(feature = "convert",
reason = "waiting on RFC revision")]
pub fn as_str(&self) -> &str {
self
}

/// Pushes the given string onto this string buffer.
///
/// # Examples
Expand Down Expand Up @@ -848,7 +856,6 @@ impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b str {
#[allow(deprecated)]
impl Str for String {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn as_slice(&self) -> &str {
unsafe { mem::transmute(&*self.vec) }
}
Expand Down
20 changes: 10 additions & 10 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,18 @@ impl<T> Vec<T> {
}
}

/// Extract a slice containing the entire vector.
#[inline]
#[unstable(feature = "convert",
reason = "waiting on RFC revision")]
pub fn as_slice(&self) -> &[T] {
self
}

/// Deprecated: use `&mut s[..]` instead.
#[inline]
#[unstable(feature = "collections",
reason = "will be replaced by slice syntax")]
#[deprecated(since = "1.0.0", reason = "use &mut s[..] instead")]
#[unstable(feature = "convert",
reason = "waiting on RFC revision")]
pub fn as_mut_slice(&mut self) -> &mut [T] {
&mut self[..]
}
Expand Down Expand Up @@ -1640,13 +1647,6 @@ impl<T> AsRef<Vec<T>> for Vec<T> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Into<Vec<T>> for Vec<T> {
fn into(self) -> Vec<T> {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<[T]> for Vec<T> {
fn as_ref(&self) -> &[T] {
Expand Down
38 changes: 22 additions & 16 deletions src/libcore/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,57 @@
//! conversions from one type to another. They follow the standard
//! Rust conventions of `as`/`to`/`into`/`from`.
#![unstable(feature = "convert",
reason = "recently added, experimental traits")]
#![stable(feature = "rust1", since = "1.0.0")]

use marker::Sized;

/// A cheap, reference-to-reference conversion.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait AsRef<T: ?Sized> {
/// Perform the conversion.
#[stable(feature = "rust1", since = "1.0.0")]
fn as_ref(&self) -> &T;
}

/// A cheap, mutable reference-to-mutable reference conversion.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait AsMut<T: ?Sized> {
/// Perform the conversion.
#[stable(feature = "rust1", since = "1.0.0")]
fn as_mut(&mut self) -> &mut T;
}

/// A conversion that consumes `self`, which may or may not be
/// expensive.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Into<T>: Sized {
/// Perform the conversion.
#[stable(feature = "rust1", since = "1.0.0")]
fn into(self) -> T;
}

/// Construct `Self` via a conversion.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait From<T> {
/// Perform the conversion.
#[stable(feature = "rust1", since = "1.0.0")]
fn from(T) -> Self;
}

////////////////////////////////////////////////////////////////////////////////
// GENERIC IMPLS
////////////////////////////////////////////////////////////////////////////////

// As implies Into
impl<'a, T: ?Sized, U: ?Sized> Into<&'a U> for &'a T where T: AsRef<U> {
fn into(self) -> &'a U {
self.as_ref()
}
}

// As lifts over &
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T where T: AsRef<U> {
fn as_ref(&self) -> &U {
<T as AsRef<U>>::as_ref(*self)
}
}

// As lifts over &mut
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
fn as_ref(&self) -> &U {
<T as AsRef<U>>::as_ref(*self)
Expand All @@ -77,14 +79,8 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
// }
// }

// AsMut implies Into
impl<'a, T: ?Sized, U: ?Sized> Into<&'a mut U> for &'a mut T where T: AsMut<U> {
fn into(self) -> &'a mut U {
(*self).as_mut()
}
}

// AsMut lifts over &mut
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
fn as_mut(&mut self) -> &mut U {
(*self).as_mut()
Expand All @@ -100,28 +96,38 @@ impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
// }

// From implies Into
#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U> Into<U> for T where U: From<T> {
fn into(self) -> U {
U::from(self)
}
}

// From (and thus Into) is reflexive
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> From<T> for T {
fn from(t: T) -> T { t }
}

////////////////////////////////////////////////////////////////////////////////
// CONCRETE IMPLS
////////////////////////////////////////////////////////////////////////////////

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<[T]> for [T] {
fn as_ref(&self) -> &[T] {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsMut<[T]> for [T] {
fn as_mut(&mut self) -> &mut [T] {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRef<str> for str {
fn as_ref(&self) -> &str {
self
Expand Down
1 change: 0 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#![feature(path_ext)]
#![feature(str_words)]
#![feature(str_char)]
#![feature(convert)]
#![feature(into_cow)]
#![feature(slice_patterns)]
#![cfg_attr(test, feature(test))]
Expand Down
1 change: 0 additions & 1 deletion src/librustc_back/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#![feature(path_ext)]
#![feature(std_misc)]
#![feature(step_by)]
#![feature(convert)]
#![cfg_attr(test, feature(test, rand))]

extern crate syntax;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#![feature(io)]
#![feature(set_stdio)]
#![feature(unicode)]
#![feature(convert)]

extern crate arena;
extern crate flate;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_trans/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#![feature(unicode)]
#![feature(path_ext)]
#![feature(fs)]
#![feature(convert)]
#![feature(path_relative_from)]

#![allow(trivial_casts)]
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#![feature(file_path)]
#![feature(path_ext)]
#![feature(path_relative_from)]
#![feature(convert)]
#![feature(slice_patterns)]

extern crate arena;
Expand Down
1 change: 0 additions & 1 deletion src/libserialize/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ Core encoding and decoding interfaces.
#![feature(std_misc)]
#![feature(unicode)]
#![feature(str_char)]
#![feature(convert)]
#![cfg_attr(test, feature(test, old_io))]

// test harness access
Expand Down
1 change: 0 additions & 1 deletion src/libstd/dynamic_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ mod dl {
use ffi::{CStr, OsStr};
use str;
use libc;
use os::unix::prelude::*;
use ptr;

pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
Expand Down
1 change: 0 additions & 1 deletion src/libstd/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ pub struct JoinPathsError {
/// # Examples
///
/// ```
/// # #![feature(convert)]
/// use std::env;
/// use std::path::PathBuf;
///
Expand Down
Loading

0 comments on commit 9fc51ef

Please sign in to comment.