| 166 | } |
| 167 | |
| 168 | pub async fn async_save(&self, path: impl AsRef<Path>) -> Result<(), Error> { |
| 169 | let path = path.as_ref(); |
| 170 | let path_tmp = path.with_extension("tmp"); |
| 171 | let mut fpb = tokio::fs::OpenOptions::new(); |
| 172 | let fpb = fpb.create(true).write(true).truncate(true); |
| 173 | let mut fp = fpb.open(&path_tmp).await?; |
| 174 | let state_str = toml::to_string(&self)?; |
| 175 | fp.write_all(state_str.as_bytes()).await?; |
| 176 | fp.sync_data().await?; |
| 177 | mem::drop(fp); |
| 178 | tokio::fs::rename(path_tmp, path).await?; |
| 179 | let dir = tokio::fs::File::open(path.parent().unwrap_or_else(|| Path::new("."))).await?; |
| 180 | dir.sync_all().await?; |
| 181 | Ok(()) |
| 182 | } |
| 183 | |
| 184 | pub fn from_file(path: impl AsRef<Path>, key_cache_capacity: usize) -> Result<Self, Error> { |
| 185 | let state_str = fs::read_to_string(path)?; |