Skip to content

Commit c851126

Browse files
authored
Add support for smallvec (#341)
* feat(buf): impl `IoBuf[Mut]` for smallvec * also impl io vec buf
1 parent f7f0403 commit c851126

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

compio-buf/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ rustdoc-args = ["--cfg", "docsrs"]
1818
bumpalo = { version = "3.14.0", optional = true }
1919
arrayvec = { version = "0.7.4", optional = true }
2020
bytes = { workspace = true, optional = true }
21+
smallvec = { version = "1.13.2", optional = true }
2122

2223
[target.'cfg(unix)'.dependencies]
2324
libc = { workspace = true }
2425

2526
[features]
2627
# Dependencies
2728
arrayvec = ["dep:arrayvec"]
29+
smallvec = ["dep:smallvec"]
2830
bumpalo = ["dep:bumpalo"]
2931
bytes = ["dep:bytes"]
3032

compio-buf/src/io_buf.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,24 @@ unsafe impl<const N: usize> IoBuf for arrayvec::ArrayVec<u8, N> {
305305
}
306306
}
307307

308+
#[cfg(feature = "smallvec")]
309+
unsafe impl<const N: usize> IoBuf for smallvec::SmallVec<[u8; N]>
310+
where
311+
[u8; N]: smallvec::Array<Item = u8>,
312+
{
313+
fn as_buf_ptr(&self) -> *const u8 {
314+
self.as_ptr()
315+
}
316+
317+
fn buf_len(&self) -> usize {
318+
self.len()
319+
}
320+
321+
fn buf_capacity(&self) -> usize {
322+
self.capacity()
323+
}
324+
}
325+
308326
/// A mutable compio compatible buffer.
309327
///
310328
/// The `IoBufMut` trait is implemented by buffer types that can be passed to
@@ -396,6 +414,16 @@ unsafe impl<const N: usize> IoBufMut for arrayvec::ArrayVec<u8, N> {
396414
}
397415
}
398416

417+
#[cfg(feature = "smallvec")]
418+
unsafe impl<const N: usize> IoBufMut for smallvec::SmallVec<[u8; N]>
419+
where
420+
[u8; N]: smallvec::Array<Item = u8>,
421+
{
422+
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
423+
self.as_mut_ptr()
424+
}
425+
}
426+
399427
/// A helper trait for `set_len` like methods.
400428
pub trait SetBufInit {
401429
/// Set the buffer length. If `len` is less than the current length, nothing
@@ -469,6 +497,18 @@ impl<const N: usize> SetBufInit for arrayvec::ArrayVec<u8, N> {
469497
}
470498
}
471499

500+
#[cfg(feature = "smallvec")]
501+
impl<const N: usize> SetBufInit for smallvec::SmallVec<[u8; N]>
502+
where
503+
[u8; N]: smallvec::Array<Item = u8>,
504+
{
505+
unsafe fn set_buf_init(&mut self, len: usize) {
506+
if (**self).buf_len() < len {
507+
self.set_len(len);
508+
}
509+
}
510+
}
511+
472512
impl<T: IoBufMut> SetBufInit for [T] {
473513
unsafe fn set_buf_init(&mut self, len: usize) {
474514
default_set_buf_init(self.iter_mut(), len)
@@ -496,6 +536,16 @@ impl<T: IoBufMut, const N: usize> SetBufInit for arrayvec::ArrayVec<T, N> {
496536
}
497537
}
498538

539+
#[cfg(feature = "smallvec")]
540+
impl<T: IoBufMut, const N: usize> SetBufInit for smallvec::SmallVec<[T; N]>
541+
where
542+
[T; N]: smallvec::Array<Item = T>,
543+
{
544+
unsafe fn set_buf_init(&mut self, len: usize) {
545+
default_set_buf_init(self.iter_mut(), len)
546+
}
547+
}
548+
499549
unsafe fn default_set_buf_init<'a, B: IoBufMut>(
500550
iter: impl IntoIterator<Item = &'a mut B>,
501551
mut len: usize,

compio-buf/src/io_vec_buf.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ops::{Deref, DerefMut};
22

33
use crate::{
4-
IndexedIter, IoBuf, IoBufMut, IoSlice, IoSliceMut, OwnedIterator, SetBufInit, t_alloc,
4+
t_alloc, IndexedIter, IoBuf, IoBufMut, IoSlice, IoSliceMut, OwnedIterator, SetBufInit,
55
};
66

77
/// A type that's either owned or borrowed. Like [`Cow`](std::rc::Cow) but
@@ -149,6 +149,23 @@ impl<T: IoBuf, const N: usize> IoVectoredBuf for arrayvec::ArrayVec<T, N> {
149149
}
150150
}
151151

152+
#[cfg(feature = "smallvec")]
153+
impl<T: IoBuf, const N: usize> IoVectoredBuf for smallvec::SmallVec<[T; N]>
154+
where
155+
[T; N]: smallvec::Array<Item = T>,
156+
{
157+
type Buf = T;
158+
type OwnedIter = IndexedIter<Self>;
159+
160+
fn iter_buf(&self) -> impl Iterator<Item = MaybeOwned<'_, T>> {
161+
self.iter().map(MaybeOwned::Borrowed)
162+
}
163+
164+
fn owned_iter(self) -> Result<Self::OwnedIter, Self> {
165+
IndexedIter::new(self)
166+
}
167+
}
168+
152169
/// A trait for mutable vectored buffers.
153170
pub trait IoVectoredBufMut: IoVectoredBuf<Buf: IoBufMut, OwnedIter: IoBufMut> + SetBufInit {
154171
/// An iterator for the [`IoSliceMut`]s of the buffers.
@@ -195,3 +212,13 @@ impl<T: IoBufMut, const N: usize> IoVectoredBufMut for arrayvec::ArrayVec<T, N>
195212
self.iter_mut().map(MaybeOwnedMut::Borrowed)
196213
}
197214
}
215+
216+
#[cfg(feature = "smallvec")]
217+
impl<T: IoBufMut, const N: usize> IoVectoredBufMut for smallvec::SmallVec<[T; N]>
218+
where
219+
[T; N]: smallvec::Array<Item = T>,
220+
{
221+
fn iter_buf_mut(&mut self) -> impl Iterator<Item = MaybeOwnedMut<'_, Self::Buf>> {
222+
self.iter_mut().map(MaybeOwnedMut::Borrowed)
223+
}
224+
}

compio-buf/src/iter.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ impl<T, const N: usize> Indexable for arrayvec::ArrayVec<T, N> {
163163
}
164164
}
165165

166+
#[cfg(feature = "smallvec")]
167+
impl<T, const N: usize> Indexable for smallvec::SmallVec<[T; N]>
168+
where
169+
[T; N]: smallvec::Array<Item = T>,
170+
{
171+
type Output = T;
172+
173+
fn index(&self, n: usize) -> Option<&T> {
174+
self.get(n)
175+
}
176+
}
177+
166178
impl<T> IndexableMut for &mut [T] {
167179
fn index_mut(&mut self, n: usize) -> Option<&mut T> {
168180
self.get_mut(n)
@@ -195,3 +207,13 @@ impl<T, const N: usize> IndexableMut for arrayvec::ArrayVec<T, N> {
195207
self.get_mut(n)
196208
}
197209
}
210+
211+
#[cfg(feature = "smallvec")]
212+
impl<T, const N: usize> IndexableMut for smallvec::SmallVec<[T; N]>
213+
where
214+
[T; N]: smallvec::Array<Item = T>,
215+
{
216+
fn index_mut(&mut self, n: usize) -> Option<&mut T> {
217+
self.get_mut(n)
218+
}
219+
}

0 commit comments

Comments
 (0)