Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
chore(buffer): make constraint in buffer shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li committed Jul 11, 2022
1 parent 890853c commit 2e98d16
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/buffer/immutable.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::{iter::FromIterator, sync::Arc, usize};

use crate::types::NativeType;

use super::bytes::Bytes;

/// [`Buffer`] is a contiguous memory region of plain old data types
/// that can be shared across thread boundaries.
///
/// The easiest way to think about [`Buffer<T>`] is being equivalent to
/// a `Arc<Vec<T>>`, with the following differences:
/// * `T` must be [`NativeType`]
/// * `T` must be [`std::fmt::Debug`]
/// * slicing and cloning is `O(1)`.
/// * it supports external allocated memory (via FFI)
///
Expand All @@ -34,7 +32,7 @@ use super::bytes::Bytes;
/// assert_eq!(buffer.get_mut(), None);
/// ```
#[derive(Clone, PartialEq)]
pub struct Buffer<T: NativeType> {
pub struct Buffer<T: std::fmt::Debug> {
/// the internal byte buffer.
data: Arc<Bytes<T>>,

Expand All @@ -46,20 +44,20 @@ pub struct Buffer<T: NativeType> {
length: usize,
}

impl<T: NativeType> std::fmt::Debug for Buffer<T> {
impl<T: std::fmt::Debug> std::fmt::Debug for Buffer<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&**self, f)
}
}

impl<T: NativeType> Default for Buffer<T> {
impl<T: std::fmt::Debug> Default for Buffer<T> {
#[inline]
fn default() -> Self {
Vec::new().into()
}
}

impl<T: NativeType> Buffer<T> {
impl<T: std::fmt::Debug> Buffer<T> {
/// Creates an empty [`Buffer`].
#[inline]
pub fn new() -> Self {
Expand Down Expand Up @@ -162,7 +160,7 @@ impl<T: NativeType> Buffer<T> {
}
}

impl<T: NativeType> From<Vec<T>> for Buffer<T> {
impl<T: std::fmt::Debug> From<Vec<T>> for Buffer<T> {
#[inline]
fn from(p: Vec<T>) -> Self {
let bytes: Bytes<T> = p.into();
Expand All @@ -174,7 +172,7 @@ impl<T: NativeType> From<Vec<T>> for Buffer<T> {
}
}

impl<T: NativeType> std::ops::Deref for Buffer<T> {
impl<T: std::fmt::Debug> std::ops::Deref for Buffer<T> {
type Target = [T];

#[inline]
Expand All @@ -183,7 +181,7 @@ impl<T: NativeType> std::ops::Deref for Buffer<T> {
}
}

impl<T: NativeType> FromIterator<T> for Buffer<T> {
impl<T: std::fmt::Debug> FromIterator<T> for Buffer<T> {
#[inline]
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Vec::from_iter(iter).into()
Expand Down

0 comments on commit 2e98d16

Please sign in to comment.