| 1 | //! Filesystem manipulation operations. |
| 2 | |
| 3 | use std::fs::File; |
| 4 | use std::io::{Result, Write}; |
| 5 | use std::path::Path; |
| 6 | |
| 7 | use crate::Close; |
| 8 | |
| 9 | /// Write a slice as the entire contents of a file. |
| 10 | /// |
| 11 | /// This function is identical to [`std::fs::write`] but uses [`Close`] |
| 12 | /// to drop the [`File`](std::fs::File) created from path, returning any |
| 13 | /// errors encountered when doing so. |
| 14 | pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>( |
| 15 | path: P, |
| 16 | contents: C, |
| 17 | ) -> Result<()> { |
| 18 | fn inner(path: &Path, contents: &[u8]) -> Result<()> { |
| 19 | let mut f = File::create(path)?; |
| 20 | f.write_all(contents)?; |
| 21 | f.close() |
| 22 | } |
| 23 | inner(path.as_ref(), contents.as_ref()) |
| 24 | } |