Skip to content

Commit

Permalink
add a few inlines to buffer types (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus authored Jan 1, 2024
1 parent 3b94218 commit 7bfef09
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions buffer/src/fragments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl<'sval> TextBuf<'sval> {
/**
Create a new empty text buffer.
*/
#[inline(always)]
pub fn new() -> Self {
TextBuf {
buf: FragmentBuf::new(""),
Expand Down Expand Up @@ -59,13 +60,15 @@ impl<'sval> TextBuf<'sval> {
/**
Clear the text buffer so it can be re-used.
*/
#[inline(always)]
pub fn clear(&mut self) {
*self = Default::default();
}

/**
Push a borrowed text fragment onto the buffer.
*/
#[inline(always)]
pub fn push_fragment(&mut self, fragment: &'sval str) -> Result<(), Error> {
self.buf.push(fragment)
}
Expand All @@ -76,6 +79,7 @@ impl<'sval> TextBuf<'sval> {
If the `std` feature of this library is enabled, this method will
buffer the fragment. In no-std environments this method will fail.
*/
#[inline(always)]
pub fn push_fragment_computed(&mut self, fragment: &str) -> Result<(), Error> {
self.buf.push_computed(fragment)
}
Expand Down Expand Up @@ -119,13 +123,15 @@ impl<'sval> TextBuf<'sval> {
/**
Try get the contents of the buffer as a string borrowed for the `'sval` lifetime.
*/
#[inline(always)]
pub fn as_borrowed_str(&self) -> Option<&'sval str> {
self.buf.as_borrowed_inner()
}

/**
Get the contents of the buffer as a string.
*/
#[inline(always)]
pub fn as_str(&self) -> &str {
self.buf.as_inner()
}
Expand Down Expand Up @@ -217,12 +223,14 @@ impl<'sval> fmt::Write for TextBuf<'sval> {
}

impl<'sval> Default for TextBuf<'sval> {
#[inline(always)]
fn default() -> Self {
TextBuf::new()
}
}

impl<'sval> From<&'sval str> for TextBuf<'sval> {
#[inline(always)]
fn from(fragment: &'sval str) -> Self {
TextBuf {
buf: FragmentBuf::new(fragment),
Expand All @@ -231,6 +239,7 @@ impl<'sval> From<&'sval str> for TextBuf<'sval> {
}

impl<'sval> AsRef<str> for TextBuf<'sval> {
#[inline(always)]
fn as_ref(&self) -> &str {
self.as_str()
}
Expand Down Expand Up @@ -272,6 +281,7 @@ impl<'sval> BinaryBuf<'sval> {
/**
Create a new empty binary buffer.
*/
#[inline(always)]
pub fn new() -> Self {
BinaryBuf {
buf: FragmentBuf::new(&[]),
Expand All @@ -297,13 +307,15 @@ impl<'sval> BinaryBuf<'sval> {
/**
Clear the binary buffer so it can be re-used.
*/
#[inline(always)]
pub fn clear(&mut self) {
*self = Default::default();
}

/**
Push a borrowed binary fragment onto the buffer.
*/
#[inline(always)]
pub fn push_fragment(&mut self, fragment: &'sval [u8]) -> Result<(), Error> {
self.buf.push(fragment)
}
Expand All @@ -314,20 +326,23 @@ impl<'sval> BinaryBuf<'sval> {
If the `std` feature of this library is enabled, this method will
buffer the fragment. In no-std environments this method will fail.
*/
#[inline(always)]
pub fn push_fragment_computed(&mut self, fragment: &[u8]) -> Result<(), Error> {
self.buf.push_computed(fragment)
}

/**
Try get the contents of the buffer as a slice borrowed for the `'sval` lifetime.
*/
#[inline(always)]
pub fn as_borrowed_slice(&self) -> Option<&'sval [u8]> {
self.buf.as_borrowed_inner()
}

/**
Get the contents of the buffer as a slice.
*/
#[inline(always)]
pub fn as_slice(&self) -> &[u8] {
self.buf.as_inner()
}
Expand Down Expand Up @@ -436,12 +451,14 @@ impl<'a> sval::Stream<'a> for BinaryCollector<'a> {
}

impl<'sval> Default for BinaryBuf<'sval> {
#[inline(always)]
fn default() -> Self {
BinaryBuf::new()
}
}

impl<'sval> From<&'sval [u8]> for BinaryBuf<'sval> {
#[inline(always)]
fn from(fragment: &'sval [u8]) -> Self {
BinaryBuf {
buf: FragmentBuf::new(fragment),
Expand All @@ -458,6 +475,7 @@ impl<'sval, const N: usize> From<&'sval [u8; N]> for BinaryBuf<'sval> {
}

impl<'sval> AsRef<[u8]> for BinaryBuf<'sval> {
#[inline(always)]
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
Expand All @@ -480,6 +498,7 @@ impl<'sval> sval_ref::ValueRef<'sval> for BinaryBuf<'sval> {

#[cfg(not(feature = "alloc"))]
trait Fragment {
#[inline(always)]
fn to_fragment<'sval>(&'sval self) -> &'sval Self {
self
}
Expand All @@ -489,6 +508,7 @@ trait Fragment {

#[cfg(feature = "alloc")]
trait Fragment: ToOwned {
#[inline(always)]
fn to_fragment<'sval>(&'sval self) -> Cow<'sval, Self> {
Cow::Borrowed(self)
}
Expand All @@ -509,6 +529,7 @@ trait Fragment: ToOwned {

impl Fragment for str {
#[cfg(feature = "alloc")]
#[inline(always)]
fn extend(buf: &mut Cow<Self>, fragment: &Self) {
buf.to_mut().push_str(fragment);
}
Expand All @@ -520,6 +541,7 @@ impl Fragment for str {

impl Fragment for [u8] {
#[cfg(feature = "alloc")]
#[inline(always)]
fn extend(buf: &mut Cow<Self>, fragment: &Self) {
buf.to_mut().extend(fragment);
}
Expand Down Expand Up @@ -596,12 +618,14 @@ impl<'sval, T: ?Sized + Fragment + PartialEq> Eq for FragmentBuf<'sval, T> {}
impl<'sval, T: ?Sized + Fragment + Eq> Eq for FragmentBuf<'sval, T> where T::Owned: Eq {}

impl<'sval, T: ?Sized + Fragment> FragmentBuf<'sval, T> {
#[inline(always)]
fn new(value: &'sval T) -> Self {
FragmentBuf {
value: value.to_fragment(),
}
}

#[inline(always)]
fn push(&mut self, fragment: &'sval T) -> Result<(), Error> {
if self.value.can_replace() {
self.value = fragment.to_fragment();
Expand All @@ -612,6 +636,7 @@ impl<'sval, T: ?Sized + Fragment> FragmentBuf<'sval, T> {
}
}

#[inline(always)]
fn push_computed(&mut self, fragment: &T) -> Result<(), Error> {
#[cfg(feature = "alloc")]
{
Expand All @@ -627,6 +652,7 @@ impl<'sval, T: ?Sized + Fragment> FragmentBuf<'sval, T> {
}
}

#[inline(always)]
fn as_borrowed_inner(&self) -> Option<&'sval T> {
#[cfg(feature = "alloc")]
{
Expand All @@ -642,6 +668,7 @@ impl<'sval, T: ?Sized + Fragment> FragmentBuf<'sval, T> {
}
}

#[inline(always)]
fn as_inner(&self) -> &T {
#[cfg(feature = "alloc")]
{
Expand Down

0 comments on commit 7bfef09

Please sign in to comment.