Skip to content

Commit 84ed77b

Browse files
authored
Merge pull request #277 from George-Miao/refactor/io-vec-buf
refactor: `IoVectoredBuf`
2 parents cf93207 + 566addf commit 84ed77b

File tree

12 files changed

+379
-378
lines changed

12 files changed

+379
-378
lines changed

compio-buf/src/io_buf.rs

Lines changed: 0 additions & 290 deletions
Original file line numberDiff line numberDiff line change
@@ -396,296 +396,6 @@ unsafe impl<const N: usize> IoBufMut for arrayvec::ArrayVec<u8, N> {
396396
}
397397
}
398398

399-
/// A trait for vectored buffers.
400-
pub trait IoVectoredBuf: Sized + 'static {
401-
/// An iterator for the [`IoSlice`]s of the buffers.
402-
///
403-
/// # Safety
404-
///
405-
/// The return slice will not live longer than self.
406-
/// It is static to provide convenience from writing self-referenced
407-
/// structure.
408-
unsafe fn as_io_slices(&self) -> Vec<IoSlice> {
409-
self.as_dyn_bufs().map(|buf| buf.as_io_slice()).collect()
410-
}
411-
412-
/// Iterate the inner buffers.
413-
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf>;
414-
415-
/// Create an owned iterator to make it easy to pass this vectored buffer as
416-
/// a regular buffer.
417-
///
418-
/// ```
419-
/// use compio_buf::{IoBuf, IoVectoredBuf};
420-
///
421-
/// let bufs = [vec![1u8, 2], vec![3, 4]];
422-
/// let iter = bufs.owned_iter().unwrap();
423-
/// assert_eq!(iter.as_slice(), &[1, 2]);
424-
/// let iter = iter.next().unwrap();
425-
/// assert_eq!(iter.as_slice(), &[3, 4]);
426-
/// let iter = iter.next();
427-
/// assert!(iter.is_err());
428-
/// ```
429-
///
430-
/// The time complexity of the returned iterator depends on the
431-
/// implementation of [`Iterator::nth`] of [`IoVectoredBuf::as_dyn_bufs`].
432-
fn owned_iter(self) -> Result<OwnedIter<impl OwnedIterator<Inner = Self>>, Self>;
433-
}
434-
435-
impl<T: IoBuf> IoVectoredBuf for &'static [T] {
436-
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf> {
437-
self.iter().map(|buf| buf as &dyn IoBuf)
438-
}
439-
440-
fn owned_iter(self) -> Result<OwnedIter<impl OwnedIterator<Inner = Self>>, Self> {
441-
IndexedIter::new(self, 0).map(OwnedIter::new)
442-
}
443-
}
444-
445-
impl<T: IoBuf> IoVectoredBuf for &'static mut [T] {
446-
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf> {
447-
self.iter().map(|buf| buf as &dyn IoBuf)
448-
}
449-
450-
fn owned_iter(self) -> Result<OwnedIter<impl OwnedIterator<Inner = Self>>, Self> {
451-
IndexedIter::new(self, 0).map(OwnedIter::new)
452-
}
453-
}
454-
455-
impl<T: IoVectoredBuf + IoIndexedBuf> IoVectoredBuf for &'static T {
456-
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf> {
457-
(**self).as_dyn_bufs()
458-
}
459-
460-
fn owned_iter(self) -> Result<OwnedIter<impl OwnedIterator<Inner = Self>>, Self> {
461-
IndexedIter::new(self, 0).map(OwnedIter::new)
462-
}
463-
}
464-
465-
impl<T: IoVectoredBuf + IoIndexedBuf> IoVectoredBuf for &'static mut T {
466-
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf> {
467-
(**self).as_dyn_bufs()
468-
}
469-
470-
fn owned_iter(self) -> Result<OwnedIter<impl OwnedIterator<Inner = Self>>, Self> {
471-
IndexedIter::new(self, 0).map(OwnedIter::new)
472-
}
473-
}
474-
475-
impl<T: IoBuf, const N: usize> IoVectoredBuf for [T; N] {
476-
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf> {
477-
self.iter().map(|buf| buf as &dyn IoBuf)
478-
}
479-
480-
fn owned_iter(self) -> Result<OwnedIter<impl OwnedIterator<Inner = Self>>, Self> {
481-
IndexedIter::new(self, 0).map(OwnedIter::new)
482-
}
483-
}
484-
485-
impl<T: IoBuf, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoVectoredBuf
486-
for t_alloc!(Vec, T, A)
487-
{
488-
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf> {
489-
self.iter().map(|buf| buf as &dyn IoBuf)
490-
}
491-
492-
fn owned_iter(self) -> Result<OwnedIter<impl OwnedIterator<Inner = Self>>, Self> {
493-
IndexedIter::new(self, 0).map(OwnedIter::new)
494-
}
495-
}
496-
497-
#[cfg(feature = "arrayvec")]
498-
impl<T: IoBuf, const N: usize> IoVectoredBuf for arrayvec::ArrayVec<T, N> {
499-
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf> {
500-
self.iter().map(|buf| buf as &dyn IoBuf)
501-
}
502-
503-
fn owned_iter(self) -> Result<OwnedIter<impl OwnedIterator<Inner = Self>>, Self> {
504-
IndexedIter::new(self, 0).map(OwnedIter::new)
505-
}
506-
}
507-
508-
/// A trait for mutable vectored buffers.
509-
pub trait IoVectoredBufMut: IoVectoredBuf + SetBufInit {
510-
/// An iterator for the [`IoSliceMut`]s of the buffers.
511-
///
512-
/// # Safety
513-
///
514-
/// The return slice will not live longer than self.
515-
/// It is static to provide convenience from writing self-referenced
516-
/// structure.
517-
unsafe fn as_io_slices_mut(&mut self) -> Vec<IoSliceMut> {
518-
self.as_dyn_mut_bufs()
519-
.map(|buf| buf.as_io_slice_mut())
520-
.collect()
521-
}
522-
523-
/// Iterate the inner buffers.
524-
fn as_dyn_mut_bufs(&mut self) -> impl Iterator<Item = &mut dyn IoBufMut>;
525-
526-
/// Create an owned iterator to make it easy to pass this vectored buffer as
527-
/// a regular buffer.
528-
///
529-
/// ```
530-
/// use compio_buf::{IoBuf, IoVectoredBufMut};
531-
///
532-
/// let bufs = [vec![1u8, 2], vec![3, 4]];
533-
/// let iter = bufs.owned_iter_mut().unwrap();
534-
/// assert_eq!(iter.as_slice(), &[1, 2]);
535-
/// let iter = iter.next().unwrap();
536-
/// assert_eq!(iter.as_slice(), &[3, 4]);
537-
/// let iter = iter.next();
538-
/// assert!(iter.is_err());
539-
/// ```
540-
///
541-
/// The time complexity of the returned iterator depends on the
542-
/// implementation of [`Iterator::nth`] of [`IoVectoredBuf::as_dyn_bufs`].
543-
fn owned_iter_mut(self) -> Result<OwnedIter<impl OwnedIteratorMut<Inner = Self>>, Self>;
544-
}
545-
546-
impl<T: IoBufMut> IoVectoredBufMut for &'static mut [T] {
547-
fn as_dyn_mut_bufs(&mut self) -> impl Iterator<Item = &mut dyn IoBufMut> {
548-
self.iter_mut().map(|buf| buf as &mut dyn IoBufMut)
549-
}
550-
551-
fn owned_iter_mut(self) -> Result<OwnedIter<impl OwnedIteratorMut<Inner = Self>>, Self> {
552-
IndexedIter::new(self, 0).map(OwnedIter::new)
553-
}
554-
}
555-
556-
impl<T: IoVectoredBufMut + IoIndexedBufMut> IoVectoredBufMut for &'static mut T {
557-
fn as_dyn_mut_bufs(&mut self) -> impl Iterator<Item = &mut dyn IoBufMut> {
558-
(**self).as_dyn_mut_bufs()
559-
}
560-
561-
fn owned_iter_mut(self) -> Result<OwnedIter<impl OwnedIteratorMut<Inner = Self>>, Self> {
562-
IndexedIter::new(self, 0).map(OwnedIter::new)
563-
}
564-
}
565-
566-
impl<T: IoBufMut, const N: usize> IoVectoredBufMut for [T; N] {
567-
fn as_dyn_mut_bufs(&mut self) -> impl Iterator<Item = &mut dyn IoBufMut> {
568-
self.iter_mut().map(|buf| buf as &mut dyn IoBufMut)
569-
}
570-
571-
fn owned_iter_mut(self) -> Result<OwnedIter<impl OwnedIteratorMut<Inner = Self>>, Self> {
572-
IndexedIter::new(self, 0).map(OwnedIter::new)
573-
}
574-
}
575-
576-
impl<T: IoBufMut, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoVectoredBufMut
577-
for t_alloc!(Vec, T, A)
578-
{
579-
fn as_dyn_mut_bufs(&mut self) -> impl Iterator<Item = &mut dyn IoBufMut> {
580-
self.iter_mut().map(|buf| buf as &mut dyn IoBufMut)
581-
}
582-
583-
fn owned_iter_mut(self) -> Result<OwnedIter<impl OwnedIteratorMut<Inner = Self>>, Self> {
584-
IndexedIter::new(self, 0).map(OwnedIter::new)
585-
}
586-
}
587-
588-
#[cfg(feature = "arrayvec")]
589-
impl<T: IoBufMut, const N: usize> IoVectoredBufMut for arrayvec::ArrayVec<T, N> {
590-
fn as_dyn_mut_bufs(&mut self) -> impl Iterator<Item = &mut dyn IoBufMut> {
591-
self.iter_mut().map(|buf| buf as &mut dyn IoBufMut)
592-
}
593-
594-
fn owned_iter_mut(self) -> Result<OwnedIter<impl OwnedIteratorMut<Inner = Self>>, Self> {
595-
IndexedIter::new(self, 0).map(OwnedIter::new)
596-
}
597-
}
598-
599-
/// A trait for vectored buffers that could be indexed.
600-
pub trait IoIndexedBuf: IoVectoredBuf {
601-
/// Get the buffer with specific index.
602-
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf>;
603-
}
604-
605-
impl<T: IoBuf> IoIndexedBuf for &'static [T] {
606-
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf> {
607-
self.get(n).map(|b| b as _)
608-
}
609-
}
610-
611-
impl<T: IoBuf> IoIndexedBuf for &'static mut [T] {
612-
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf> {
613-
self.get(n).map(|b| b as _)
614-
}
615-
}
616-
617-
impl<T: IoIndexedBuf> IoIndexedBuf for &'static T {
618-
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf> {
619-
(**self).buf_nth(n)
620-
}
621-
}
622-
623-
impl<T: IoIndexedBuf> IoIndexedBuf for &'static mut T {
624-
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf> {
625-
(**self).buf_nth(n)
626-
}
627-
}
628-
629-
impl<T: IoBuf, const N: usize> IoIndexedBuf for [T; N] {
630-
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf> {
631-
self.get(n).map(|b| b as _)
632-
}
633-
}
634-
635-
impl<T: IoBuf, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoIndexedBuf
636-
for t_alloc!(Vec, T, A)
637-
{
638-
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf> {
639-
self.get(n).map(|b| b as _)
640-
}
641-
}
642-
643-
#[cfg(feature = "arrayvec")]
644-
impl<T: IoBuf, const N: usize> IoIndexedBuf for arrayvec::ArrayVec<T, N> {
645-
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf> {
646-
self.get(n).map(|b| b as _)
647-
}
648-
}
649-
650-
/// A trait for mutable vectored buffers that could be indexed.
651-
pub trait IoIndexedBufMut: IoVectoredBufMut + IoIndexedBuf {
652-
/// Get the mutable buffer with specific index.
653-
fn buf_nth_mut(&mut self, n: usize) -> Option<&mut dyn IoBufMut>;
654-
}
655-
656-
impl<T: IoBufMut> IoIndexedBufMut for &'static mut [T] {
657-
fn buf_nth_mut(&mut self, n: usize) -> Option<&mut dyn IoBufMut> {
658-
self.get_mut(n).map(|b| b as _)
659-
}
660-
}
661-
662-
impl<T: IoIndexedBufMut> IoIndexedBufMut for &'static mut T {
663-
fn buf_nth_mut(&mut self, n: usize) -> Option<&mut dyn IoBufMut> {
664-
(**self).buf_nth_mut(n)
665-
}
666-
}
667-
668-
impl<T: IoBufMut, const N: usize> IoIndexedBufMut for [T; N] {
669-
fn buf_nth_mut(&mut self, n: usize) -> Option<&mut dyn IoBufMut> {
670-
self.get_mut(n).map(|b| b as _)
671-
}
672-
}
673-
674-
impl<T: IoBufMut, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoIndexedBufMut
675-
for t_alloc!(Vec, T, A)
676-
{
677-
fn buf_nth_mut(&mut self, n: usize) -> Option<&mut dyn IoBufMut> {
678-
self.get_mut(n).map(|b| b as _)
679-
}
680-
}
681-
682-
#[cfg(feature = "arrayvec")]
683-
impl<T: IoBufMut, const N: usize> IoIndexedBufMut for arrayvec::ArrayVec<T, N> {
684-
fn buf_nth_mut(&mut self, n: usize) -> Option<&mut dyn IoBufMut> {
685-
self.get_mut(n).map(|b| b as _)
686-
}
687-
}
688-
689399
/// A helper trait for `set_len` like methods.
690400
pub trait SetBufInit {
691401
/// Set the buffer length. If `len` is less than the current length, nothing

0 commit comments

Comments
 (0)