Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 0 additions & 126 deletions core/io/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,6 @@ impl From<bytes::Bytes> for BufView {
}
}

/// BufMutViewWhole is equivalent to `BufMutView`, but cannot be split, preventing
/// someone from accidentally holding a `BufView` down the road that is being actively
/// mutated from JavaScript.
pub struct BufMutViewWhole {
inner: BufMutViewInner,
cursor: usize,
}

/// BufMutView is a wrapper around an underlying contiguous chunk of writable
/// bytes. It can be created from a `JsBuffer` or a `Vec<u8>` and implements
/// `DerefMut<[u8]>` and `AsMut<[u8]>`.
Expand Down Expand Up @@ -419,124 +411,6 @@ impl From<BytesMut> for BufMutView {
}
}

impl BufMutViewWhole {
fn from_inner(inner: BufMutViewInner) -> Self {
Self { inner, cursor: 0 }
}

pub fn new(len: usize) -> Self {
let bytes = BytesMut::zeroed(len);
Self::from_inner(BufMutViewInner::Bytes(bytes))
}

/// Get the length of the buffer view. This is the length of the underlying
/// buffer minus the cursor position.
pub fn len(&self) -> usize {
match &self.inner {
BufMutViewInner::JsBuffer(js_buf) => js_buf.len() - self.cursor,
BufMutViewInner::Bytes(bytes) => bytes.len() - self.cursor,
}
}

/// Is the buffer view empty?
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Advance the internal cursor of the buffer view by `n` bytes.
pub fn advance_cursor(&mut self, n: usize) {
assert!(self.len() >= n);
self.cursor += n;
}

/// Reset the internal cursor of the buffer view to the beginning of the
/// buffer. Returns the old cursor position.
pub fn reset_cursor(&mut self) -> usize {
let old = self.cursor;
self.cursor = 0;
old
}

/// Turn this `BufMutView` into a `BufView`.
pub fn into_view(self) -> BufView {
let inner = match self.inner {
BufMutViewInner::JsBuffer(js_buf) => BufViewInner::JsBuffer(js_buf),
BufMutViewInner::Bytes(bytes) => BufViewInner::Bytes(bytes.into()),
};
BufView {
inner,
cursor: self.cursor,
}
}

/// Attempts to unwrap the underlying buffer into a [`BytesMut`], consuming the `BufMutView`. If
/// this buffer does not have a [`BytesMut`], returns `Self`.
pub fn maybe_unwrap_bytes(self) -> Result<BytesMut, Self> {
match self.inner {
BufMutViewInner::JsBuffer(_) => Err(self),
BufMutViewInner::Bytes(bytes) => Ok(bytes),
}
}

/// Adjust the length of the remaining buffer and ensure that the cursor continues to
/// stay in-bounds.
pub fn truncate(&mut self, size: usize) {
match &mut self.inner {
BufMutViewInner::Bytes(bytes) => bytes.truncate(size + self.cursor),
BufMutViewInner::JsBuffer(buffer) => buffer.truncate(size + self.cursor),
}
self.cursor = std::cmp::min(self.cursor, self.len());
}
}

impl Buf for BufMutViewWhole {
fn remaining(&self) -> usize {
self.len()
}

fn chunk(&self) -> &[u8] {
self.deref()
}

fn advance(&mut self, cnt: usize) {
self.advance_cursor(cnt)
}
}

impl Deref for BufMutViewWhole {
type Target = [u8];

fn deref(&self) -> &[u8] {
let buf = match &self.inner {
BufMutViewInner::JsBuffer(js_buf) => js_buf.deref(),
BufMutViewInner::Bytes(vec) => vec.deref(),
};
&buf[self.cursor..]
}
}

impl DerefMut for BufMutViewWhole {
fn deref_mut(&mut self) -> &mut [u8] {
let buf = match &mut self.inner {
BufMutViewInner::JsBuffer(js_buf) => js_buf.deref_mut(),
BufMutViewInner::Bytes(vec) => vec.deref_mut(),
};
&mut buf[self.cursor..]
}
}

impl From<JsBuffer> for BufMutViewWhole {
fn from(buf: JsBuffer) -> Self {
Self::from_inner(BufMutViewInner::JsBuffer(buf.into_parts()))
}
}

impl From<BytesMut> for BufMutViewWhole {
fn from(buf: BytesMut) -> Self {
Self::from_inner(BufMutViewInner::Bytes(buf))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 0 additions & 1 deletion core/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mod resource_table;

pub use buffer_strategy::AdaptiveBufferStrategy;
pub use buffers::BufMutView;
pub use buffers::BufMutViewWhole;
pub use buffers::BufView;
pub use resource::Resource;
pub use resource_handle::ResourceHandle;
Expand Down
1 change: 0 additions & 1 deletion core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ pub use crate::inspector::JsRuntimeInspector;
pub use crate::inspector::LocalInspectorSession;
pub use crate::io::AsyncResult;
pub use crate::io::BufMutView;
pub use crate::io::BufMutViewWhole;
pub use crate::io::BufView;
pub use crate::io::Resource;
pub use crate::io::ResourceHandle;
Expand Down