1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-03 16:44:06 +02:00

remove extra indirection

This commit is contained in:
Robin Appelman 2020-12-06 01:11:54 +01:00
commit 2ab7a2216e

View file

@ -10,6 +10,7 @@ use num_traits::{Float, PrimInt};
use crate::endianness::Endianness; use crate::endianness::Endianness;
use crate::num_traits::{IsSigned, UncheckedPrimitiveFloat, UncheckedPrimitiveInt}; use crate::num_traits::{IsSigned, UncheckedPrimitiveFloat, UncheckedPrimitiveInt};
use crate::{BitError, Result}; use crate::{BitError, Result};
use std::borrow::Borrow;
use std::convert::TryInto; use std::convert::TryInto;
use std::rc::Rc; use std::rc::Rc;
@ -19,14 +20,14 @@ const USIZE_BIT_SIZE: usize = USIZE_SIZE * 8;
// Cow<[u8]> but with cheap clones using Rc // Cow<[u8]> but with cheap clones using Rc
enum Data<'a> { enum Data<'a> {
Borrowed(&'a [u8]), Borrowed(&'a [u8]),
Owned(Rc<Vec<u8>>), Owned(Rc<[u8]>),
} }
impl<'a> Data<'a> { impl<'a> Data<'a> {
pub fn as_slice(&self) -> &[u8] { pub fn as_slice(&self) -> &[u8] {
match self { match self {
Data::Borrowed(bytes) => *bytes, Data::Borrowed(bytes) => *bytes,
Data::Owned(bytes) => bytes.as_slice(), Data::Owned(bytes) => bytes.borrow(),
} }
} }
@ -145,7 +146,7 @@ where
/// ``` /// ```
pub fn new_owned(bytes: Vec<u8>, _endianness: E) -> Self { pub fn new_owned(bytes: Vec<u8>, _endianness: E) -> Self {
let byte_len = bytes.len(); let byte_len = bytes.len();
let bytes = Data::Owned(Rc::new(bytes)); let bytes = Data::Owned(Rc::from(bytes));
// this is safe because // this is safe because
// - the slice can only be access trough this struct // - the slice can only be access trough this struct
@ -710,7 +711,7 @@ impl<'a, E: Endianness> From<&'a [u8]> for BitReadBuffer<'a, E> {
impl<'a, E: Endianness> From<Vec<u8>> for BitReadBuffer<'a, E> { impl<'a, E: Endianness> From<Vec<u8>> for BitReadBuffer<'a, E> {
fn from(bytes: Vec<u8>) -> Self { fn from(bytes: Vec<u8>) -> Self {
let byte_len = bytes.len(); let byte_len = bytes.len();
let bytes = Data::Owned(Rc::new(bytes)); let bytes = Data::Owned(Rc::from(bytes));
let slice = unsafe { std::slice::from_raw_parts(bytes.as_slice().as_ptr(), bytes.len()) }; let slice = unsafe { std::slice::from_raw_parts(bytes.as_slice().as_ptr(), bytes.len()) };
BitReadBuffer { BitReadBuffer {