polars_io/utils/
sync_on_close.rs

1use std::{fs, io};
2
3#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub enum SyncOnCloseType {
6    /// Don't call sync on close.
7    #[default]
8    None,
9
10    /// Sync only the file contents.
11    Data,
12    /// Synce the file contents and the metadata.
13    All,
14}
15
16pub fn sync_on_close(sync_on_close: SyncOnCloseType, file: &mut fs::File) -> io::Result<()> {
17    match sync_on_close {
18        SyncOnCloseType::None => Ok(()),
19        SyncOnCloseType::Data => file.sync_data(),
20        SyncOnCloseType::All => file.sync_all(),
21    }
22}
23
24#[cfg(feature = "tokio")]
25pub async fn tokio_sync_on_close(
26    sync_on_close: SyncOnCloseType,
27    file: &mut tokio::fs::File,
28) -> io::Result<()> {
29    match sync_on_close {
30        SyncOnCloseType::None => Ok(()),
31        SyncOnCloseType::Data => file.sync_data().await,
32        SyncOnCloseType::All => file.sync_all().await,
33    }
34}