-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
/// | ||
/// # 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 | ||
|
@@ -62,12 +80,32 @@ impl OsString { | |
/// Converts the `OsString` into a `String` if it contains valid Unicode data. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And in here for |
||
/// | ||
/// 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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 { | ||
|
@@ -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() | ||
|
@@ -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() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.