Skip to content

Commit

Permalink
Don't require an Encoder type while constructing FramedWrite so we ca…
Browse files Browse the repository at this point in the history
…n be generic over the lifetime
  • Loading branch information
w4 committed Oct 13, 2021
1 parent 0309633 commit 916359e
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions actix/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,17 @@ where

/// A wrapper for the `AsyncWrite` and `Encoder` types. The [`AsyncWrite`] will be flushed when this
/// struct is dropped.
pub struct FramedWrite<I, T: AsyncWrite + Unpin, U: Encoder<I>> {
pub struct FramedWrite<T: AsyncWrite + Unpin, U, E: From<io::Error> = std::io::Error> {
enc: U,
inner: UnsafeWriter<T, U::Error>,
inner: UnsafeWriter<T, E>,
}

impl<I, T: AsyncWrite + Unpin, U: Encoder<I>> FramedWrite<I, T, U> {
impl<T: AsyncWrite + Unpin, U, E: From<io::Error>> FramedWrite<T, U, E> {
pub fn new<A, C>(io: T, enc: U, ctx: &mut C) -> Self
where
A: Actor<Context = C> + WriteHandler<U::Error>,
A: Actor<Context = C> + WriteHandler<E>,
C: AsyncContext<A>,
U::Error: 'static,
E: 'static,
T: Unpin + 'static,
{
let inner = UnsafeWriter(
Expand All @@ -336,9 +336,9 @@ impl<I, T: AsyncWrite + Unpin, U: Encoder<I>> FramedWrite<I, T, U> {

pub fn from_buffer<A, C>(io: T, enc: U, buffer: BytesMut, ctx: &mut C) -> Self
where
A: Actor<Context = C> + WriteHandler<U::Error>,
A: Actor<Context = C> + WriteHandler<E>,
C: AsyncContext<A>,
U::Error: 'static,
E: 'static,
T: Unpin + 'static,
{
let inner = UnsafeWriter(
Expand Down Expand Up @@ -382,7 +382,10 @@ impl<I, T: AsyncWrite + Unpin, U: Encoder<I>> FramedWrite<I, T, U> {
}

/// Writes an item to the sink.
pub fn write(&mut self, item: I) {
pub fn write<I>(&mut self, item: I)
where
U: Encoder<I, Error = E>,
{
let mut inner = self.inner.0.borrow_mut();
let _ = self.enc.encode(item, &mut inner.buffer).map_err(|e| {
inner.error = Some(e);
Expand All @@ -396,9 +399,19 @@ impl<I, T: AsyncWrite + Unpin, U: Encoder<I>> FramedWrite<I, T, U> {
pub fn handle(&self) -> SpawnHandle {
self.inner.0.borrow().handle
}

/// Returns a reference to the underlying encoder.
pub fn encoder(&self) -> &U {
&self.enc
}

/// Returns a mutable reference to the underlying encoder.
pub fn encoder_mut(&mut self) -> &mut U {
&mut self.enc
}
}

impl<I, T: AsyncWrite + Unpin, U: Encoder<I>> Drop for FramedWrite<I, T, U> {
impl<T: AsyncWrite + Unpin, U, E: From<io::Error>> Drop for FramedWrite<T, U, E> {
fn drop(&mut self) {
// Attempts to write any remaining bytes to the stream and flush it
let mut async_writer = self.inner.1.borrow_mut();
Expand Down

0 comments on commit 916359e

Please sign in to comment.