Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-io/src/utils/sync_on_close.rs
6939 views
1
use std::{fs, io};
2
3
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
4
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
6
pub enum SyncOnCloseType {
7
/// Don't call sync on close.
8
#[default]
9
None,
10
11
/// Sync only the file contents.
12
Data,
13
/// Synce the file contents and the metadata.
14
All,
15
}
16
17
pub fn sync_on_close(sync_on_close: SyncOnCloseType, file: &mut fs::File) -> io::Result<()> {
18
match sync_on_close {
19
SyncOnCloseType::None => Ok(()),
20
SyncOnCloseType::Data => file.sync_data(),
21
SyncOnCloseType::All => file.sync_all(),
22
}
23
}
24
25
#[cfg(feature = "tokio")]
26
pub async fn tokio_sync_on_close(
27
sync_on_close: SyncOnCloseType,
28
file: &mut tokio::fs::File,
29
) -> io::Result<()> {
30
match sync_on_close {
31
SyncOnCloseType::None => Ok(()),
32
SyncOnCloseType::Data => file.sync_data().await,
33
SyncOnCloseType::All => file.sync_all().await,
34
}
35
}
36
37