Skip to content

Commit

Permalink
crates/core/bytestream/writer: Generalize writer trait for std writer…
Browse files Browse the repository at this point in the history
…s and feature gate in that no-std writers are disabled on writing
  • Loading branch information
etemesi254 committed Apr 5, 2024
1 parent 6e667d0 commit a36143e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crates/zune-core/src/bytestream/writer/no_std_writer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// We cannot use the below impls and std ones because we'll re-implement the
// same trait fot &[u8] which is blanketed by write. Ending up with two separate implementations
#![cfg(not(feature = "std"))]
use crate::bytestream::{ZByteIoError, ZByteWriterTrait};

impl ZByteWriterTrait for &mut [u8] {
Expand Down
8 changes: 5 additions & 3 deletions crates/zune-core/src/bytestream/writer/std_writer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![cfg(feature = "std")]
use std::fs::File;
use std::io::{BufWriter, Write};

use std::io::Write;

use crate::bytestream::ZByteIoError;

impl crate::bytestream::ZByteWriterTrait for &mut BufWriter<File> {
impl<T: Write> crate::bytestream::ZByteWriterTrait for T {
fn write_bytes(&mut self, buf: &[u8]) -> Result<usize, ZByteIoError> {
self.write(buf).map_err(ZByteIoError::StdIoError)
}
Expand All @@ -20,6 +20,8 @@ impl crate::bytestream::ZByteWriterTrait for &mut BufWriter<File> {
self.flush().map_err(ZByteIoError::StdIoError)
}
fn reserve_capacity(&mut self, _: usize) -> Result<(), ZByteIoError> {
// we can't reserve capacity, sorry to implementations where this
// matters
Ok(())
}
}

0 comments on commit a36143e

Please sign in to comment.