add byte buf access method to VersionBox

This commit is contained in:
2021-02-13 16:06:14 +01:00
parent 871e1088ef
commit fb2505b4b1

View File

@@ -1,5 +1,5 @@
use bytes::Buf;
use serde::{Deserialize, Serialize};
use serde_bytes;
use std::{borrow::Cow, convert::TryFrom, fmt, io::IoSlice};
use uuid::Uuid;
@@ -70,6 +70,14 @@ impl VersionBytes {
pub fn buf(&self) -> VersionBytesBuf<'_> {
VersionBytesBuf::new(self.0, &self.1)
}
pub fn from_slice(slice: &[u8]) -> Result<VersionBytes, ParseError> {
TryFrom::try_from(slice)
}
pub fn to_vec(&self) -> Vec<u8> {
self.as_version_bytes_ref().to_vec()
}
}
impl From<VersionBytes> for Vec<u8> {
@@ -141,6 +149,22 @@ impl<'a> VersionBytesRef<'a> {
pub fn buf(&self) -> VersionBytesBuf<'_> {
VersionBytesBuf::new(self.0, &self.1)
}
pub fn from_slice(slice: &'a [u8]) -> Result<VersionBytesRef<'a>, ParseError> {
TryFrom::try_from(slice)
}
pub fn to_vec(&self) -> Vec<u8> {
let mut buf = self.buf();
let mut vec = Vec::with_capacity(buf.remaining());
while buf.has_remaining() {
let chunk = buf.chunk();
vec.extend_from_slice(chunk);
let chunk_len = chunk.len();
buf.advance(chunk_len);
}
vec
}
}
impl<'a> AsRef<[u8]> for VersionBytesRef<'a> {
@@ -204,7 +228,7 @@ impl<'a> VersionBytesBuf<'a> {
}
}
impl<'a> ::bytes::Buf for VersionBytesBuf<'a> {
impl<'a> Buf for VersionBytesBuf<'a> {
fn remaining(&self) -> usize {
VERSION_LEN + self.content.len() - self.pos
}