Skip to content

Add doc examples for std::ffi::OsString fucntions/methods. #39221

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

Merged
merged 1 commit into from
Jan 22, 2017
Merged
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
73 changes: 73 additions & 0 deletions src/libstd/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,30 @@ pub struct OsStr {

impl OsString {
/// Constructs a new empty `OsString`.
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let os_string = OsString::new();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> OsString {
OsString { inner: Buf::from_string(String::new()) }
}

/// Converts to an `OsStr` slice.
Copy link
Member

Choose a reason for hiding this comment

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

Can you add the url here please?

Copy link
Member Author

Choose a reason for hiding this comment

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

Linking these URLs is tangential to my changes in this pull request.

Copy link
Member

Choose a reason for hiding this comment

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

Yes but we tend to forget otherwise. However if you open another PR which does it, I'm perfectly fine with it.

///
/// # Examples
///
/// ```
/// use std::ffi::{OsString, OsStr};
///
/// let os_string = OsString::from("foo");
/// let os_str = OsStr::new("foo");
/// assert_eq!(os_string.as_os_str(), os_str);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn as_os_str(&self) -> &OsStr {
self
Expand All @@ -62,12 +80,32 @@ impl OsString {
/// Converts the `OsString` into a `String` if it contains valid Unicode data.
Copy link
Member

Choose a reason for hiding this comment

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

And in here for String?

///
/// On failure, ownership of the original `OsString` is returned.
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let os_string = OsString::from("foo");
/// let string = os_string.into_string();
/// assert_eq!(string, Ok(String::from("foo")));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_string(self) -> Result<String, OsString> {
self.inner.into_string().map_err(|buf| OsString { inner: buf} )
}

/// Extends the string with the given `&OsStr` slice.
Copy link
Member

Choose a reason for hiding this comment

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

In here as well?

///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut os_string = OsString::from("foo");
/// os_string.push("bar");
/// assert_eq!(&os_string, "foobar");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
self.inner.push_slice(&s.as_ref().inner)
Expand All @@ -80,6 +118,20 @@ impl OsString {
/// allocate.
///
/// See main `OsString` documentation information about encoding.
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut os_string = OsString::with_capacity(10);
/// let capacity = os_string.capacity();
///
/// // This push is done without reallocating
/// os_string.push("foo");
///
/// assert_eq!(capacity, os_string.capacity());
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
pub fn with_capacity(capacity: usize) -> OsString {
OsString {
Expand All @@ -88,6 +140,18 @@ impl OsString {
}

/// Truncates the `OsString` to zero length.
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut os_string = OsString::from("foo");
/// assert_eq!(&os_string, "foo");
///
/// os_string.clear();
/// assert_eq!(&os_string, "");
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
pub fn clear(&mut self) {
self.inner.clear()
Expand All @@ -96,6 +160,15 @@ impl OsString {
/// Returns the capacity this `OsString` can hold without reallocating.
///
/// See `OsString` introduction for information about encoding.
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
///
/// let mut os_string = OsString::with_capacity(10);
/// assert!(os_string.capacity() >= 10);
/// ```
#[stable(feature = "osstring_simple_functions", since = "1.9.0")]
pub fn capacity(&self) -> usize {
self.inner.capacity()
Expand Down