refactor(tvix/castore): move magic number to B3_LEN const

… and export it.

Change-Id: I47d2dc2f5a8174da65c614b43801d648506e2d73
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9544
Tested-by: BuildkiteCI
Reviewed-by: edef <edef@edef.eu>
Autosubmit: flokli <flokli@flokli.de>
This commit is contained in:
Florian Klink 2023-10-05 10:57:27 +03:00 committed by flokli
parent 8239f32b62
commit c67ab911eb
2 changed files with 7 additions and 5 deletions

View file

@ -12,6 +12,8 @@ pub enum Error {
InvalidDigestLen(usize), InvalidDigestLen(usize),
} }
pub const B3_LEN: usize = 32;
impl B3Digest { impl B3Digest {
// returns a copy of the inner [Vec<u8>]. // returns a copy of the inner [Vec<u8>].
pub fn to_vec(&self) -> Vec<u8> { pub fn to_vec(&self) -> Vec<u8> {
@ -31,7 +33,7 @@ impl TryFrom<Vec<u8>> for B3Digest {
// constructs a [B3Digest] from a [Vec<u8>]. // constructs a [B3Digest] from a [Vec<u8>].
// Returns an error if the digest has the wrong length. // Returns an error if the digest has the wrong length.
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> { fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
if value.len() != 32 { if value.len() != B3_LEN {
Err(Error::InvalidDigestLen(value.len())) Err(Error::InvalidDigestLen(value.len()))
} else { } else {
Ok(Self(value.into())) Ok(Self(value.into()))
@ -45,7 +47,7 @@ impl TryFrom<bytes::Bytes> for B3Digest {
// constructs a [B3Digest] from a [bytes::Bytes]. // constructs a [B3Digest] from a [bytes::Bytes].
// Returns an error if the digest has the wrong length. // Returns an error if the digest has the wrong length.
fn try_from(value: bytes::Bytes) -> Result<Self, Self::Error> { fn try_from(value: bytes::Bytes) -> Result<Self, Self::Error> {
if value.len() != 32 { if value.len() != B3_LEN {
Err(Error::InvalidDigestLen(value.len())) Err(Error::InvalidDigestLen(value.len()))
} else { } else {
Ok(Self(value)) Ok(Self(value))
@ -53,8 +55,8 @@ impl TryFrom<bytes::Bytes> for B3Digest {
} }
} }
impl From<&[u8; 32]> for B3Digest { impl From<&[u8; B3_LEN]> for B3Digest {
fn from(value: &[u8; 32]) -> Self { fn from(value: &[u8; B3_LEN]) -> Self {
Self(value.to_vec().into()) Self(value.to_vec().into())
} }
} }

View file

@ -8,7 +8,7 @@ pub mod import;
pub mod proto; pub mod proto;
pub mod utils; pub mod utils;
pub use digests::B3Digest; pub use digests::{B3Digest, B3_LEN};
pub use errors::Error; pub use errors::Error;
#[cfg(test)] #[cfg(test)]