Skip to content

Commit d69161c

Browse files
Rollup merge of #35087 - GuillaumeGomez:fs_docs, r=steveklabnik
Fs docs Fixes #29356. r? @steveklabnik
2 parents 3f06bf9 + 123bf1e commit d69161c

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/libstd/fs.rs

+51
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,23 @@ impl Metadata {
694694
///
695695
/// This field may not be available on all platforms, and will return an
696696
/// `Err` on platforms where it is not available.
697+
///
698+
/// # Examples
699+
///
700+
/// ```
701+
/// # fn foo() -> std::io::Result<()> {
702+
/// use std::fs;
703+
///
704+
/// let metadata = try!(fs::metadata("foo.txt"));
705+
///
706+
/// if let Ok(time) = metadata.modified() {
707+
/// println!("{:?}", time);
708+
/// } else {
709+
/// println!("Not supported on this platform");
710+
/// }
711+
/// # Ok(())
712+
/// # }
713+
/// ```
697714
#[stable(feature = "fs_time", since = "1.10.0")]
698715
pub fn modified(&self) -> io::Result<SystemTime> {
699716
self.0.modified().map(FromInner::from_inner)
@@ -712,6 +729,23 @@ impl Metadata {
712729
///
713730
/// This field may not be available on all platforms, and will return an
714731
/// `Err` on platforms where it is not available.
732+
///
733+
/// # Examples
734+
///
735+
/// ```
736+
/// # fn foo() -> std::io::Result<()> {
737+
/// use std::fs;
738+
///
739+
/// let metadata = try!(fs::metadata("foo.txt"));
740+
///
741+
/// if let Ok(time) = metadata.accessed() {
742+
/// println!("{:?}", time);
743+
/// } else {
744+
/// println!("Not supported on this platform");
745+
/// }
746+
/// # Ok(())
747+
/// # }
748+
/// ```
715749
#[stable(feature = "fs_time", since = "1.10.0")]
716750
pub fn accessed(&self) -> io::Result<SystemTime> {
717751
self.0.accessed().map(FromInner::from_inner)
@@ -726,6 +760,23 @@ impl Metadata {
726760
///
727761
/// This field may not be available on all platforms, and will return an
728762
/// `Err` on platforms where it is not available.
763+
///
764+
/// # Examples
765+
///
766+
/// ```
767+
/// # fn foo() -> std::io::Result<()> {
768+
/// use std::fs;
769+
///
770+
/// let metadata = try!(fs::metadata("foo.txt"));
771+
///
772+
/// if let Ok(time) = metadata.created() {
773+
/// println!("{:?}", time);
774+
/// } else {
775+
/// println!("Not supported on this platform");
776+
/// }
777+
/// # Ok(())
778+
/// # }
779+
/// ```
729780
#[stable(feature = "fs_time", since = "1.10.0")]
730781
pub fn created(&self) -> io::Result<SystemTime> {
731782
self.0.created().map(FromInner::from_inner)

src/libstd/sys/unix/ext/fs.rs

+50
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,53 @@ use sys::platform::fs::MetadataExt as UnixMetadataExt;
2525
pub trait PermissionsExt {
2626
/// Returns the underlying raw `mode_t` bits that are the standard Unix
2727
/// permissions for this file.
28+
///
29+
/// # Examples
30+
///
31+
/// ```rust,ignore
32+
/// use std::fs::File;
33+
/// use std::os::unix::fs::PermissionsExt;
34+
///
35+
/// let f = try!(File::create("foo.txt"));
36+
/// let metadata = try!(f.metadata());
37+
/// let permissions = metadata.permissions();
38+
///
39+
/// println!("permissions: {}", permissions.mode());
40+
/// ```
2841
#[stable(feature = "fs_ext", since = "1.1.0")]
2942
fn mode(&self) -> u32;
3043

3144
/// Sets the underlying raw bits for this set of permissions.
45+
///
46+
/// # Examples
47+
///
48+
/// ```rust,ignore
49+
/// use std::fs::File;
50+
/// use std::os::unix::fs::PermissionsExt;
51+
///
52+
/// let f = try!(File::create("foo.txt"));
53+
/// let metadata = try!(f.metadata());
54+
/// let mut permissions = metadata.permissions();
55+
///
56+
/// permissions.set_mode(0o644); // Read/write for owner and read for others.
57+
/// assert_eq!(permissions.mode(), 0o644);
58+
/// ```
3259
#[stable(feature = "fs_ext", since = "1.1.0")]
3360
fn set_mode(&mut self, mode: u32);
3461

3562
/// Creates a new instance of `Permissions` from the given set of Unix
3663
/// permission bits.
64+
///
65+
/// # Examples
66+
///
67+
/// ```rust,ignore
68+
/// use std::fs::Permissions;
69+
/// use std::os::unix::fs::PermissionsExt;
70+
///
71+
/// // Read/write for owner and read for others.
72+
/// let permissions = Permissions::from_mode(0o644);
73+
/// assert_eq!(permissions.mode(), 0o644);
74+
/// ```
3775
#[stable(feature = "fs_ext", since = "1.1.0")]
3876
fn from_mode(mode: u32) -> Self;
3977
}
@@ -63,6 +101,18 @@ pub trait OpenOptionsExt {
63101
/// If no `mode` is set, the default of `0o666` will be used.
64102
/// The operating system masks out bits with the systems `umask`, to produce
65103
/// the final permissions.
104+
///
105+
/// # Examples
106+
///
107+
/// ```rust,ignore
108+
/// extern crate libc;
109+
/// use std::fs::OpenOptions;
110+
/// use std::os::unix::fs::OpenOptionsExt;
111+
///
112+
/// let mut options = OpenOptions::new();
113+
/// options.mode(0o644); // Give read/write for owner and read for others.
114+
/// let file = options.open("foo.txt");
115+
/// ```
66116
#[stable(feature = "fs_ext", since = "1.1.0")]
67117
fn mode(&mut self, mode: u32) -> &mut Self;
68118

0 commit comments

Comments
 (0)