use bytes::{Bytes, BytesMut};
use crate::registry;
use crate::IntoInner;
pub struct TrackedBytesMut {
inner: BytesMut,
#[allow(dead_code)]
name: &'static str,
file: &'static str,
line: u32,
column: u32,
}
impl TrackedBytesMut {
pub fn with_capacity_named(
cap: usize,
name: &'static str,
file: &'static str,
line: u32,
column: u32,
) -> Self {
registry::record_creation(name, file, line, column);
Self {
inner: BytesMut::with_capacity(cap),
name,
file,
line,
column,
}
}
#[inline]
pub fn wrap_from(
inner: BytesMut,
name: &'static str,
file: &'static str,
line: u32,
column: u32,
) -> Self {
registry::record_creation(name, file, line, column);
Self {
inner,
name,
file,
line,
column,
}
}
}
impl std::ops::Deref for TrackedBytesMut {
type Target = BytesMut;
fn deref(&self) -> &BytesMut {
&self.inner
}
}
impl std::ops::DerefMut for TrackedBytesMut {
fn deref_mut(&mut self) -> &mut BytesMut {
&mut self.inner
}
}
impl Drop for TrackedBytesMut {
fn drop(&mut self) {
registry::record_sample(self.file, self.line, self.column, self.inner.capacity());
}
}
impl From<TrackedBytesMut> for BytesMut {
fn from(tracked: TrackedBytesMut) -> BytesMut {
registry::record_sample(
tracked.file,
tracked.line,
tracked.column,
tracked.inner.capacity(),
);
let inner = unsafe { std::ptr::read(&tracked.inner) };
std::mem::forget(tracked);
inner
}
}
impl IntoInner for TrackedBytesMut {
type Inner = BytesMut;
#[inline]
fn into_inner(self) -> BytesMut {
BytesMut::from(self)
}
}
impl TrackedBytesMut {
#[inline]
pub fn freeze(self) -> Bytes {
registry::record_sample(self.file, self.line, self.column, self.inner.capacity());
let inner = unsafe { std::ptr::read(&self.inner) };
std::mem::forget(self);
inner.freeze()
}
}