From 779fad29a12e3dd5206d5a5488ddce517390132e Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Mon, 31 Jul 2023 23:12:31 +0200 Subject: refactor(tvix/nix-compat/derivation): use writer.write_all This is more concise than a io::copy of a Cursor to bytes, and we have everything to be written in memory. Change-Id: I81f34666aa61aef4e16b33423ce4a69c3781efc3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8997 Autosubmit: flokli Reviewed-by: raitobezarius Tested-by: BuildkiteCI --- tvix/nix-compat/src/derivation/mod.rs | 2 +- tvix/nix-compat/src/derivation/write.rs | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'tvix/nix-compat/src/derivation') diff --git a/tvix/nix-compat/src/derivation/mod.rs b/tvix/nix-compat/src/derivation/mod.rs index 6a1eee4a81c3..6ee42bb3ed96 100644 --- a/tvix/nix-compat/src/derivation/mod.rs +++ b/tvix/nix-compat/src/derivation/mod.rs @@ -47,7 +47,7 @@ impl Derivation { /// /// The only errors returns are these when writing to the passed writer. pub fn serialize(&self, writer: &mut impl std::io::Write) -> Result<(), io::Error> { - io::copy(&mut io::Cursor::new(write::DERIVATION_PREFIX), writer)?; + writer.write_all(write::DERIVATION_PREFIX.as_bytes())?; write::write_char(writer, write::PAREN_OPEN)?; write::write_outputs(writer, &self.outputs)?; diff --git a/tvix/nix-compat/src/derivation/write.rs b/tvix/nix-compat/src/derivation/write.rs index 223ba9ff3fd9..5937f619b719 100644 --- a/tvix/nix-compat/src/derivation/write.rs +++ b/tvix/nix-compat/src/derivation/write.rs @@ -6,9 +6,12 @@ use crate::derivation::escape::escape_bstr; use crate::derivation::output::Output; use bstr::BString; -use std::collections::BTreeSet; -use std::io::Cursor; -use std::{collections::BTreeMap, io, io::Error, io::Write}; +use std::{ + collections::{BTreeMap, BTreeSet}, + io, + io::Error, + io::Write, +}; pub const DERIVATION_PREFIX: &str = "Derive"; pub const PAREN_OPEN: char = '('; @@ -22,8 +25,7 @@ pub const QUOTE: char = '"'; pub(crate) fn write_char(writer: &mut impl Write, c: char) -> io::Result<()> { let mut buf = [0; 4]; let b = c.encode_utf8(&mut buf).as_bytes(); - io::copy(&mut Cursor::new(b), writer)?; - Ok(()) + writer.write_all(b) } // Write a string `s` as a quoted field to the writer. @@ -38,9 +40,9 @@ pub(crate) fn write_field>( write_char(writer, QUOTE)?; if !escape { - io::copy(&mut Cursor::new(s), writer)?; + writer.write_all(s.as_ref())?; } else { - io::copy(&mut Cursor::new(escape_bstr(s.as_ref())), writer)?; + writer.write_all(&escape_bstr(s.as_ref()))?; } write_char(writer, QUOTE)?; -- cgit 1.4.1