add byte buf access method to VersionBox
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
|
use bytes::Buf;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_bytes;
|
|
||||||
use std::{borrow::Cow, convert::TryFrom, fmt, io::IoSlice};
|
use std::{borrow::Cow, convert::TryFrom, fmt, io::IoSlice};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@@ -70,6 +70,14 @@ impl VersionBytes {
|
|||||||
pub fn buf(&self) -> VersionBytesBuf<'_> {
|
pub fn buf(&self) -> VersionBytesBuf<'_> {
|
||||||
VersionBytesBuf::new(self.0, &self.1)
|
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> {
|
impl From<VersionBytes> for Vec<u8> {
|
||||||
@@ -141,6 +149,22 @@ impl<'a> VersionBytesRef<'a> {
|
|||||||
pub fn buf(&self) -> VersionBytesBuf<'_> {
|
pub fn buf(&self) -> VersionBytesBuf<'_> {
|
||||||
VersionBytesBuf::new(self.0, &self.1)
|
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> {
|
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 {
|
fn remaining(&self) -> usize {
|
||||||
VERSION_LEN + self.content.len() - self.pos
|
VERSION_LEN + self.content.len() - self.pos
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user