From a36143e15f1177e659b3e8cdbea35f0256a5b8d7 Mon Sep 17 00:00:00 2001 From: caleb Date: Sat, 6 Apr 2024 00:16:29 +0300 Subject: [PATCH] crates/core/bytestream/writer: Generalize writer trait for std writers and feature gate in that no-std writers are disabled on writing --- crates/zune-core/src/bytestream/writer/no_std_writer.rs | 3 +++ crates/zune-core/src/bytestream/writer/std_writer.rs | 8 +++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/zune-core/src/bytestream/writer/no_std_writer.rs b/crates/zune-core/src/bytestream/writer/no_std_writer.rs index ff9a29f9..fd5fca82 100644 --- a/crates/zune-core/src/bytestream/writer/no_std_writer.rs +++ b/crates/zune-core/src/bytestream/writer/no_std_writer.rs @@ -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] { diff --git a/crates/zune-core/src/bytestream/writer/std_writer.rs b/crates/zune-core/src/bytestream/writer/std_writer.rs index 8e598132..d82aba5f 100644 --- a/crates/zune-core/src/bytestream/writer/std_writer.rs +++ b/crates/zune-core/src/bytestream/writer/std_writer.rs @@ -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 { +impl crate::bytestream::ZByteWriterTrait for T { fn write_bytes(&mut self, buf: &[u8]) -> Result { self.write(buf).map_err(ZByteIoError::StdIoError) } @@ -20,6 +20,8 @@ impl crate::bytestream::ZByteWriterTrait for &mut BufWriter { 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(()) } }