tokio 1.54.1

An event-driven, non-blocking I/O platform for writing asynchronous I/O backed applications.
Documentation
use std::time::SystemTime;

#[cfg(target_vendor = "apple")]
use std::os::darwin::fs::FileTimesExt;
#[cfg(windows)]
use std::os::windows::fs::FileTimesExt;

/// Timestamps for a file.
///
/// This is a specialized version of [`std::fs::FileTimes`] for usage with
/// [`File::set_times`](crate::fs::File::set_times). Unlike the standard library version,
/// this type records the timestamps itself, so a future `io-uring` file
/// implementation can consume them without converting through the standard
/// library type.
///
/// # Examples
///
/// ```no_run
/// use tokio::fs::FileTimes;
///
/// # async fn dox() -> std::io::Result<()> {
/// let times = FileTimes::new()
///     .set_accessed(std::time::SystemTime::now())
///     .set_modified(std::time::SystemTime::now());
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
#[must_use = "must be applied to a file via `File::set_times` to have any effect"]
pub struct FileTimes {
    accessed: Option<SystemTime>,
    modified: Option<SystemTime>,
    #[cfg(any(windows, target_vendor = "apple"))]
    created: Option<SystemTime>,
}

impl FileTimes {
    /// Creates a new empty [`FileTimes`].
    ///
    /// Unset timestamps are left unchanged.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the last access time.
    pub fn set_accessed(mut self, t: SystemTime) -> Self {
        self.accessed = Some(t);
        self
    }

    /// Sets the last modification time.
    pub fn set_modified(mut self, t: SystemTime) -> Self {
        self.modified = Some(t);
        self
    }

    /// Sets the creation time.
    #[cfg(any(all(doc, docsrs), windows, target_vendor = "apple"))]
    #[cfg_attr(docsrs, doc(cfg(any(windows, target_vendor = "apple"))))]
    pub fn set_created(mut self, t: SystemTime) -> Self {
        self.created = Some(t);
        self
    }

    pub(crate) fn into_std(self) -> std::fs::FileTimes {
        let mut std_times = std::fs::FileTimes::new();
        if let Some(accessed) = self.accessed {
            std_times = std_times.set_accessed(accessed);
        }
        if let Some(modified) = self.modified {
            std_times = std_times.set_modified(modified);
        }
        #[cfg(any(windows, target_vendor = "apple"))]
        if let Some(created) = self.created {
            std_times = std_times.set_created(created);
        }
        std_times
    }
}