Simplify AsyncFileWriter::new: Ret Self

instead of `io::Result<Self>`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-08 22:29:04 +10:00
parent 911c52d8e1
commit 894f9b49f9
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 6 additions and 5 deletions

View file

@ -65,7 +65,7 @@ pub async fn download<P: AsRef<Path>>(url: &str, path: P) -> Result<(), Binstall
debug!("Downloading to file: '{}'", path.display()); debug!("Downloading to file: '{}'", path.display());
let mut bytes_stream = resp.bytes_stream(); let mut bytes_stream = resp.bytes_stream();
let mut writer = AsyncFileWriter::new(path)?; let mut writer = AsyncFileWriter::new(path);
while let Some(res) = bytes_stream.next().await { while let Some(res) = bytes_stream.next().await {
writer.write(res?).await?; writer.write(res?).await?;

View file

@ -25,7 +25,7 @@ struct AsyncFileWriterInner {
} }
impl AsyncFileWriterInner { impl AsyncFileWriterInner {
fn new(path: &Path) -> io::Result<Self> { fn new(path: &Path) -> Self {
let path = path.to_owned(); let path = path.to_owned();
let (tx, rx) = mpsc::channel::<Content>(100); let (tx, rx) = mpsc::channel::<Content>(100);
@ -60,7 +60,7 @@ impl AsyncFileWriterInner {
Ok(()) Ok(())
})); }));
Ok(Self { handle, tx }) Self { handle, tx }
} }
/// Upon error, this writer shall not be reused. /// Upon error, this writer shall not be reused.
@ -110,8 +110,9 @@ impl AsyncFileWriterInner {
pub struct AsyncFileWriter(ScopeGuard<AsyncFileWriterInner, fn(AsyncFileWriterInner), Always>); pub struct AsyncFileWriter(ScopeGuard<AsyncFileWriterInner, fn(AsyncFileWriterInner), Always>);
impl AsyncFileWriter { impl AsyncFileWriter {
pub fn new(path: &Path) -> io::Result<Self> { pub fn new(path: &Path) -> Self {
AsyncFileWriterInner::new(path).map(|inner| Self(guard(inner, AsyncFileWriterInner::abort))) let inner = AsyncFileWriterInner::new(path);
Self(guard(inner, AsyncFileWriterInner::abort))
} }
/// Upon error, this writer shall not be reused. /// Upon error, this writer shall not be reused.