Skip to content

Commit

Permalink
Expose trait externally
Browse files Browse the repository at this point in the history
  • Loading branch information
ollie-etl committed Feb 15, 2024
1 parent 232ebc2 commit bba4b45
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::fs::OpenOptions;
use crate::io::SharedFd;

use crate::runtime::driver::op::Op;
use crate::sealed::MapResult;
use crate::MapResult;
use crate::{UnsubmittedOneshot, UnsubmittedWrite};
use std::fmt;
use std::io;
Expand Down
2 changes: 1 addition & 1 deletion src/io/read.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::buf::BoundedBufMut;
use crate::io::SharedFd;
use crate::sealed::WithBuffer;
use crate::Result;
use crate::WithBuffer;

use crate::runtime::driver::op::{Completable, CqeResult, Op};
use crate::runtime::CONTEXT;
Expand Down
2 changes: 1 addition & 1 deletion src/io/readv.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::buf::BoundedBufMut;
use crate::sealed::WithBuffer;
use crate::Result;
use crate::WithBuffer;

use crate::io::SharedFd;
use crate::runtime::driver::op::{Completable, CqeResult, Op};
Expand Down
2 changes: 1 addition & 1 deletion src/io/recv_from.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::runtime::driver::op::{Completable, CqeResult, Op};
use crate::runtime::CONTEXT;
use crate::sealed::WithBuffer;
use crate::WithBuffer;
use crate::{buf::BoundedBufMut, io::SharedFd, Result};
use socket2::SockAddr;
use std::{
Expand Down
2 changes: 1 addition & 1 deletion src/io/recvmsg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::runtime::driver::op::{Completable, CqeResult, Op};
use crate::runtime::CONTEXT;
use crate::sealed::WithBuffer;
use crate::WithBuffer;
use crate::{buf::BoundedBufMut, io::SharedFd, Result};
use socket2::SockAddr;
use std::{
Expand Down
2 changes: 1 addition & 1 deletion src/io/send_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::buf::BoundedBuf;
use crate::io::SharedFd;
use crate::runtime::driver::op::{Completable, CqeResult, Op};
use crate::runtime::CONTEXT;
use crate::sealed::WithBuffer;
use crate::Result;
use crate::WithBuffer;
use socket2::SockAddr;
use std::io::IoSlice;
use std::{boxed::Box, io, net::SocketAddr};
Expand Down
2 changes: 1 addition & 1 deletion src/io/send_zc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::runtime::driver::op::{Completable, CqeResult, MultiCQEFuture, Op, Updateable};
use crate::runtime::CONTEXT;
use crate::sealed::WithBuffer;
use crate::WithBuffer;
use crate::{buf::BoundedBuf, io::SharedFd, Result};
use std::io;

Expand Down
2 changes: 1 addition & 1 deletion src/io/write.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::sealed::WithBuffer;
use crate::WithBuffer;
use crate::{buf::BoundedBuf, io::SharedFd, OneshotOutputTransform, Result, UnsubmittedOneshot};
use io_uring::cqueue::Entry;
use std::io;
Expand Down
2 changes: 1 addition & 1 deletion src/io/write_fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::buf::fixed::FixedBuf;
use crate::buf::BoundedBuf;
use crate::io::SharedFd;
use crate::runtime::driver::op::{self, Completable, Op};
use crate::sealed::WithBuffer;
use crate::Result;
use crate::WithBuffer;

use crate::runtime::CONTEXT;
use std::io;
Expand Down
2 changes: 1 addition & 1 deletion src/io/writev.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::runtime::driver::op::{Completable, CqeResult, Op};
use crate::runtime::CONTEXT;
use crate::sealed::WithBuffer;
use crate::WithBuffer;
use crate::{buf::BoundedBuf, io::SharedFd, Result};
use libc::iovec;
use std::io;
Expand Down
31 changes: 18 additions & 13 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,26 @@ impl<B> Error<B> {
}
}

pub(super) mod sealed {
/// A Specialized trait for mapping over the buffer in both sides of a Result<T,B>
pub trait MapResult<B, U> {
type Output;
fn map_buf(self, f: impl FnOnce(B) -> U) -> Self::Output;
}
mod private {
pub trait Sealed {}
}
impl<T, B> private::Sealed for std::result::Result<T, B> {}

/// Adapter trait to convert result::Result<T, E> to crate::Result<T, B> where E can be
/// converted to std::io::Error.
pub trait WithBuffer<T, B>: Sized {
fn with_buffer(self, buf: B) -> T;
}
/// A Specialized trait for mapping over the buffer in both sides of a Result<T,B>
pub trait MapResult<B, U>: private::Sealed {
/// The result type after applying the map operation
type Output;
/// Apply a function over the buffer on both sides of the result
fn map_buf(self, f: impl FnOnce(B) -> U) -> Self::Output;
}

impl<T, B, U> sealed::MapResult<B, U> for Result<T, B> {
/// Adapter trait to convert result::Result<T, E> to crate::Result<T, B> where E can be
/// converted to std::io::Error.
pub trait WithBuffer<T, B>: private::Sealed {
/// Insert a buffer into each side of the result
fn with_buffer(self, buf: B) -> T;
}
impl<T, B, U> MapResult<B, U> for Result<T, B> {
type Output = Result<T, U>;
fn map_buf(self, f: impl FnOnce(B) -> U) -> Self::Output {
match self {
Expand All @@ -86,7 +91,7 @@ impl<T, B, U> sealed::MapResult<B, U> for Result<T, B> {
}

/// Adaptor implementation for Result<T, E> to Result<T, B>.
impl<T, B, E> sealed::WithBuffer<crate::Result<T, B>, B> for std::result::Result<T, E>
impl<T, B, E> WithBuffer<crate::Result<T, B>, B> for std::result::Result<T, E>
where
E: Into<std::io::Error>,
{
Expand Down

0 comments on commit bba4b45

Please sign in to comment.