Skip to content

Commit 51fcf25

Browse files
committed
feat(buf): impl IoBuf for Rc & Arc
1 parent dbccc93 commit 51fcf25

File tree

5 files changed

+64
-50
lines changed

5 files changed

+64
-50
lines changed

compio-buf/src/io_buf.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(feature = "allocator_api")]
22
use std::alloc::Allocator;
3-
use std::mem::MaybeUninit;
3+
use std::{mem::MaybeUninit, rc::Rc, sync::Arc};
44

55
use crate::*;
66

@@ -126,7 +126,7 @@ unsafe impl<B: IoBuf + ?Sized> IoBuf for &'static mut B {
126126
}
127127

128128
unsafe impl<B: IoBuf + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf
129-
for box_alloc!(B, A)
129+
for t_alloc!(Box, B, A)
130130
{
131131
fn as_buf_ptr(&self) -> *const u8 {
132132
(**self).as_buf_ptr()
@@ -141,7 +141,41 @@ unsafe impl<B: IoBuf + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator +
141141
}
142142
}
143143

144-
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf for vec_alloc!(u8, A) {
144+
unsafe impl<B: IoBuf + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf
145+
for t_alloc!(Rc, B, A)
146+
{
147+
fn as_buf_ptr(&self) -> *const u8 {
148+
(**self).as_buf_ptr()
149+
}
150+
151+
fn buf_len(&self) -> usize {
152+
(**self).buf_len()
153+
}
154+
155+
fn buf_capacity(&self) -> usize {
156+
(**self).buf_capacity()
157+
}
158+
}
159+
160+
unsafe impl<B: IoBuf + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf
161+
for t_alloc!(Arc, B, A)
162+
{
163+
fn as_buf_ptr(&self) -> *const u8 {
164+
(**self).as_buf_ptr()
165+
}
166+
167+
fn buf_len(&self) -> usize {
168+
(**self).buf_len()
169+
}
170+
171+
fn buf_capacity(&self) -> usize {
172+
(**self).buf_capacity()
173+
}
174+
}
175+
176+
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf
177+
for t_alloc!(Vec, u8, A)
178+
{
145179
fn as_buf_ptr(&self) -> *const u8 {
146180
self.as_ptr()
147181
}
@@ -314,15 +348,15 @@ unsafe impl<B: IoBufMut + ?Sized> IoBufMut for &'static mut B {
314348
}
315349

316350
unsafe impl<B: IoBufMut + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBufMut
317-
for box_alloc!(B, A)
351+
for t_alloc!(Box, B, A)
318352
{
319353
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
320354
(**self).as_buf_mut_ptr()
321355
}
322356
}
323357

324358
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBufMut
325-
for vec_alloc!(u8, A)
359+
for t_alloc!(Vec, u8, A)
326360
{
327361
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
328362
self.as_mut_ptr()
@@ -449,7 +483,7 @@ impl<T: IoBuf, const N: usize> IoVectoredBuf for [T; N] {
449483
}
450484

451485
impl<T: IoBuf, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoVectoredBuf
452-
for vec_alloc!(T, A)
486+
for t_alloc!(Vec, T, A)
453487
{
454488
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf> {
455489
self.iter().map(|buf| buf as &dyn IoBuf)
@@ -540,7 +574,7 @@ impl<T: IoBufMut, const N: usize> IoVectoredBufMut for [T; N] {
540574
}
541575

542576
impl<T: IoBufMut, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoVectoredBufMut
543-
for vec_alloc!(T, A)
577+
for t_alloc!(Vec, T, A)
544578
{
545579
fn as_dyn_mut_bufs(&mut self) -> impl Iterator<Item = &mut dyn IoBufMut> {
546580
self.iter_mut().map(|buf| buf as &mut dyn IoBufMut)
@@ -599,7 +633,7 @@ impl<T: IoBuf, const N: usize> IoIndexedBuf for [T; N] {
599633
}
600634

601635
impl<T: IoBuf, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoIndexedBuf
602-
for vec_alloc!(T, A)
636+
for t_alloc!(Vec, T, A)
603637
{
604638
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf> {
605639
self.get(n).map(|b| b as _)
@@ -638,7 +672,7 @@ impl<T: IoBufMut, const N: usize> IoIndexedBufMut for [T; N] {
638672
}
639673

640674
impl<T: IoBufMut, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoIndexedBufMut
641-
for vec_alloc!(T, A)
675+
for t_alloc!(Vec, T, A)
642676
{
643677
fn buf_nth_mut(&mut self, n: usize) -> Option<&mut dyn IoBufMut> {
644678
self.get_mut(n).map(|b| b as _)
@@ -670,14 +704,14 @@ impl<B: SetBufInit + ?Sized> SetBufInit for &'static mut B {
670704
}
671705

672706
impl<B: SetBufInit + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> SetBufInit
673-
for box_alloc!(B, A)
707+
for t_alloc!(Box, B, A)
674708
{
675709
unsafe fn set_buf_init(&mut self, len: usize) {
676710
(**self).set_buf_init(len)
677711
}
678712
}
679713

680-
impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> SetBufInit for vec_alloc!(u8, A) {
714+
impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> SetBufInit for t_alloc!(Vec, u8, A) {
681715
unsafe fn set_buf_init(&mut self, len: usize) {
682716
if (**self).buf_len() < len {
683717
self.set_len(len);
@@ -738,7 +772,7 @@ impl<T: IoBufMut, const N: usize> SetBufInit for [T; N] {
738772
}
739773

740774
impl<T: IoBufMut, #[cfg(feature = "allocator_api")] A: Allocator + 'static> SetBufInit
741-
for vec_alloc!(T, A)
775+
for t_alloc!(Vec, T, A)
742776
{
743777
unsafe fn set_buf_init(&mut self, len: usize) {
744778
default_set_buf_init(self.iter_mut(), len)

compio-buf/src/lib.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,17 @@ pub trait IntoInner {
4444
#[cfg(not(feature = "allocator_api"))]
4545
#[macro_export]
4646
#[doc(hidden)]
47-
macro_rules! vec_alloc {
48-
($t:ty, $a:ident) => {
49-
Vec<$t>
47+
macro_rules! t_alloc {
48+
($b:tt, $t:ty, $a:ident) => {
49+
$b<$t>
5050
};
5151
}
5252

5353
#[cfg(feature = "allocator_api")]
5454
#[macro_export]
5555
#[doc(hidden)]
56-
macro_rules! vec_alloc {
57-
($t:ty, $a:ident) => {
58-
Vec<$t, $a>
59-
};
60-
}
61-
62-
#[cfg(feature = "allocator_api")]
63-
#[macro_export]
64-
#[doc(hidden)]
65-
macro_rules! box_alloc {
66-
($t:ty, $a:ident) => {
67-
Box<$t, $a>
68-
};
69-
}
70-
71-
#[cfg(not(feature = "allocator_api"))]
72-
#[macro_export]
73-
#[doc(hidden)]
74-
macro_rules! box_alloc {
75-
($t:ty, $a:ident) => {
76-
Box<$t>
56+
macro_rules! t_alloc {
57+
($b:tt, $t:ty, $a:ident) => {
58+
$b<$t, $a>
7759
};
7860
}

compio-io/src/read/ext.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "allocator_api")]
22
use std::alloc::Allocator;
33

4-
use compio_buf::{vec_alloc, BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut};
4+
use compio_buf::{t_alloc, BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut};
55

66
use crate::{util::Take, AsyncRead, AsyncReadAt, IoResult};
77

@@ -183,8 +183,8 @@ pub trait AsyncReadExt: AsyncRead {
183183
/// Read all bytes until underlying reader reaches `EOF`.
184184
async fn read_to_end<#[cfg(feature = "allocator_api")] A: Allocator + 'static>(
185185
&mut self,
186-
mut buf: vec_alloc!(u8, A),
187-
) -> BufResult<usize, vec_alloc!(u8, A)> {
186+
mut buf: t_alloc!(Vec, u8, A),
187+
) -> BufResult<usize, t_alloc!(Vec, u8, A)> {
188188
loop_read_to_end!(buf, total: usize, loop self.read(buf.slice(total..)))
189189
}
190190

@@ -268,9 +268,9 @@ pub trait AsyncReadAtExt: AsyncReadAt {
268268
/// [`read_at()`]: AsyncReadAt::read_at
269269
async fn read_to_end_at<#[cfg(feature = "allocator_api")] A: Allocator + 'static>(
270270
&self,
271-
mut buffer: vec_alloc!(u8, A),
271+
mut buffer: t_alloc!(Vec, u8, A),
272272
pos: u64,
273-
) -> BufResult<usize, vec_alloc!(u8, A)> {
273+
) -> BufResult<usize, t_alloc!(Vec, u8, A)> {
274274
loop_read_to_end!(buffer, total: u64, loop self.read_at(buffer.slice(total as usize..), pos + total))
275275
}
276276

compio-io/src/read/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
use std::alloc::Allocator;
33
use std::{io::Cursor, rc::Rc, sync::Arc};
44

5-
use compio_buf::{
6-
box_alloc, buf_try, vec_alloc, BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut,
7-
};
5+
use compio_buf::{buf_try, t_alloc, BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut};
86

97
mod buf;
108
#[macro_use]
@@ -74,7 +72,7 @@ impl<A: AsyncRead + ?Sized> AsyncRead for &mut A {
7472
}
7573

7674
impl<R: AsyncRead + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator> AsyncRead
77-
for box_alloc!(R, A)
75+
for t_alloc!(Box, R, A)
7876
{
7977
#[inline(always)]
8078
async fn read<T: IoBufMut>(&mut self, buf: T) -> BufResult<usize, T> {
@@ -181,7 +179,7 @@ impl_read_at!(@ptr &A, &mut A);
181179
impl_read_at!(@ptra Box, Rc, Arc);
182180
impl_read_at!(@slice [u8], const LEN => [u8; LEN]);
183181

184-
impl<#[cfg(feature = "allocator_api")] A: Allocator> AsyncReadAt for vec_alloc!(u8, A) {
182+
impl<#[cfg(feature = "allocator_api")] A: Allocator> AsyncReadAt for t_alloc!(Vec, u8, A) {
185183
async fn read_at<T: IoBufMut>(&self, buf: T, pos: u64) -> BufResult<usize, T> {
186184
self.as_slice().read_at(buf, pos).await
187185
}

compio-io/src/write/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::alloc::Allocator;
33
use std::io::Cursor;
44

5-
use compio_buf::{box_alloc, buf_try, vec_alloc, BufResult, IntoInner, IoBuf, IoVectoredBuf};
5+
use compio_buf::{buf_try, t_alloc, BufResult, IntoInner, IoBuf, IoVectoredBuf};
66

77
use crate::IoResult;
88

@@ -69,7 +69,7 @@ impl<A: AsyncWrite + ?Sized> AsyncWrite for &mut A {
6969
}
7070

7171
impl<W: AsyncWrite + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator> AsyncWrite
72-
for box_alloc!(W, A)
72+
for t_alloc!(Box, W, A)
7373
{
7474
async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
7575
(**self).write(buf).await
@@ -154,7 +154,7 @@ impl<A: AsyncWriteAt + ?Sized> AsyncWriteAt for &mut A {
154154
}
155155

156156
impl<W: AsyncWriteAt + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator> AsyncWriteAt
157-
for box_alloc!(W, A)
157+
for t_alloc!(Box, W, A)
158158
{
159159
async fn write_at<T: IoBuf>(&mut self, buf: T, pos: u64) -> BufResult<usize, T> {
160160
(**self).write_at(buf, pos).await
@@ -190,7 +190,7 @@ impl_write_at!([u8], const LEN => [u8; LEN]);
190190
/// This implementation aligns the behavior of files. If `pos` is larger than
191191
/// the vector length, the vectored will be extended, and the extended area will
192192
/// be filled with 0.
193-
impl<#[cfg(feature = "allocator_api")] A: Allocator> AsyncWriteAt for vec_alloc!(u8, A) {
193+
impl<#[cfg(feature = "allocator_api")] A: Allocator> AsyncWriteAt for t_alloc!(Vec, u8, A) {
194194
async fn write_at<T: IoBuf>(&mut self, buf: T, pos: u64) -> BufResult<usize, T> {
195195
let pos = pos as usize;
196196
let slice = buf.as_slice();

0 commit comments

Comments
 (0)