Skip to content

Commit 8df55dd

Browse files
committed
Implemented PathBuf::set_file_name
1 parent 54c94b7 commit 8df55dd

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/path/pathbuf.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,44 @@ impl PathBuf {
152152
pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
153153
self.inner.set_extension(extension)
154154
}
155+
156+
/// Updates [`self.file_name`] to `file_name`.
157+
///
158+
/// If [`self.file_name`] was [`None`], this is equivalent to pushing
159+
/// `file_name`.
160+
///
161+
/// Otherwise it is equivalent to calling [`pop`] and then pushing
162+
/// `file_name`. The new path will be a sibling of the original path.
163+
/// (That is, it will have the same parent.)
164+
///
165+
/// [`self.file_name`]: struct.PathBuf.html#method.file_name
166+
/// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
167+
/// [`pop`]: struct.PathBuf.html#method.pop
168+
///
169+
/// # Examples
170+
///
171+
/// ```
172+
/// use async_std::path::PathBuf;
173+
///
174+
/// let mut buf = PathBuf::from("/");
175+
/// assert!(buf.file_name() == None);
176+
/// buf.set_file_name("bar");
177+
/// assert!(buf == PathBuf::from("/bar"));
178+
/// assert!(buf.file_name().is_some());
179+
/// buf.set_file_name("baz.txt");
180+
/// assert!(buf == PathBuf::from("/baz.txt"));
181+
/// ```
182+
pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
183+
self.inner.set_file_name(file_name)
184+
}
185+
}
186+
187+
impl std::ops::Deref for PathBuf {
188+
type Target = Path;
189+
190+
fn deref(&self) -> &Path {
191+
self.as_ref()
192+
}
155193
}
156194

157195
impl From<std::path::PathBuf> for PathBuf {

0 commit comments

Comments
 (0)