use std::future::Future;
use crate::fs::File;
use crate::io;
use crate::path::Path;
use crate::task::spawn_blocking;
#[derive(Clone, Debug)]
pub struct OpenOptions(std::fs::OpenOptions);
impl OpenOptions {
pub fn new() -> OpenOptions {
OpenOptions(std::fs::OpenOptions::new())
}
pub fn read(&mut self, read: bool) -> &mut OpenOptions {
self.0.read(read);
self
}
pub fn write(&mut self, write: bool) -> &mut OpenOptions {
self.0.write(write);
self
}
pub fn append(&mut self, append: bool) -> &mut OpenOptions {
self.0.append(append);
self
}
pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
self.0.truncate(truncate);
self
}
pub fn create(&mut self, create: bool) -> &mut OpenOptions {
self.0.create(create);
self
}
pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
self.0.create_new(create_new);
self
}
pub fn open<P: AsRef<Path>>(&self, path: P) -> impl Future<Output = io::Result<File>> {
let path = path.as_ref().to_owned();
let options = self.0.clone();
async move {
let file = spawn_blocking(move || options.open(path)).await?;
Ok(File::new(file, true))
}
}
}
impl Default for OpenOptions {
fn default() -> Self {
Self::new()
}
}
cfg_unix! {
use crate::os::unix::fs::OpenOptionsExt;
impl OpenOptionsExt for OpenOptions {
fn mode(&mut self, mode: u32) -> &mut Self {
self.0.mode(mode);
self
}
fn custom_flags(&mut self, flags: i32) -> &mut Self {
self.0.custom_flags(flags);
self
}
}
}