refactor(tvix/store): make nixbase32 reversal more idiomatic

Change-Id: Ibe550f9573b0f45ee95453b50c7510e49b07c719
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7685
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
This commit is contained in:
edef 2022-12-30 13:53:23 +00:00
parent 60bdf619a3
commit 51243007f6

View file

@ -36,8 +36,7 @@ impl Nixbase32Encoding {
/// Returns encoded input /// Returns encoded input
pub fn encode(&self, input: &[u8]) -> String { pub fn encode(&self, input: &[u8]) -> String {
// Reverse the input, reading in the bytes in reverse order. // Reverse the input, reading in the bytes in reverse order.
let mut reversed = Vec::with_capacity(input.len()); let reversed: Vec<u8> = input.iter().cloned().rev().collect();
reversed.extend(input.iter().rev());
self.encoding.encode(&reversed) self.encoding.encode(&reversed)
} }
@ -45,11 +44,9 @@ impl Nixbase32Encoding {
/// Check [data_encoding::Encoding::encode] for the error cases. /// Check [data_encoding::Encoding::encode] for the error cases.
pub fn decode(&self, input: &[u8]) -> Result<Vec<u8>, DecodeError> { pub fn decode(&self, input: &[u8]) -> Result<Vec<u8>, DecodeError> {
// Decode first, then reverse the bytes of the output. // Decode first, then reverse the bytes of the output.
let output = self.encoding.decode(input)?; let mut output = self.encoding.decode(&input)?;
output.reverse();
let mut reversed = Vec::with_capacity(output.len()); Ok(output)
reversed.extend(output.iter().rev());
Ok(reversed)
} }
/// Returns the decoded length of an input of length len. /// Returns the decoded length of an input of length len.