Skip to content

Commit

Permalink
[packetline #178] refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 26, 2021
1 parent b3fd65d commit 0c7c599
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
5 changes: 5 additions & 0 deletions git-packetline/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
- `immutable::Band` -> `BandRef`
- `immutable::DecodeBandError` -> `decode::band::Error`
- `pub immutable::` -> `line::`
- `pub write::` -> `write::`

* **removals**
- `write::Writer` (is now only `Writer`)
- `read::StreamingPeekableIter` (is now only `StreamingPeekableIter`)
35 changes: 24 additions & 11 deletions git-packetline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ pub enum Channel {
Error = 3,
}

mod line;
///
pub mod line;
pub mod read;

///
#[cfg(any(feature = "async-io", feature = "blocking-io"))]
mod write;
#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
pub use write::async_io::Writer;
#[cfg(feature = "blocking-io")]
pub use write::blocking_io::Writer;

/// A borrowed packet line as it refers to a slice of data by reference.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
Expand Down Expand Up @@ -62,17 +71,21 @@ pub enum BandRef<'a> {
Error(&'a [u8]),
}

/// Read pack lines one after another, without consuming more than needed from the underlying
/// [`Read`][std::io::Read]. [`Flush`][PacketLineRef::Flush] lines cause the reader to stop producing lines forever,
/// leaving [`Read`][std::io::Read] at the start of whatever comes next.
///
pub mod read;
#[doc(inline)]
pub use read::StreamingPeekableIter;

///
#[cfg(any(feature = "async-io", feature = "blocking-io"))]
pub mod write;
#[cfg(any(feature = "async-io", feature = "blocking-io"))]
#[doc(inline)]
pub use write::Writer;
/// This implementation tries hard not to allocate at all which leads to quite some added complexity and plenty of extra memory copies.
pub struct StreamingPeekableIter<T> {
read: T,
peek_buf: Vec<u8>,
#[cfg(any(feature = "blocking-io", feature = "async-io"))]
buf: Vec<u8>,
fail_on_err_lines: bool,
delimiters: &'static [PacketLineRef<'static>],
is_done: bool,
stopped_at: Option<PacketLineRef<'static>>,
}

/// Utilities to help decoding packet lines
pub mod decode;
Expand Down
18 changes: 1 addition & 17 deletions git-packetline/src/read/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(any(feature = "blocking-io", feature = "async-io"))]
use crate::MAX_LINE_LEN;
use crate::{PacketLineRef, U16_HEX_BYTES};
use crate::{PacketLineRef, StreamingPeekableIter, U16_HEX_BYTES};

#[cfg(any(feature = "blocking-io", feature = "async-io"))]
type ExhaustiveOutcome<'a> = (
Expand All @@ -9,22 +9,6 @@ type ExhaustiveOutcome<'a> = (
Option<std::io::Result<Result<PacketLineRef<'a>, crate::decode::Error>>>, // actual method result
);

/// Read pack lines one after another, without consuming more than needed from the underlying
/// [`Read`][std::io::Read]. [`Flush`][PacketLineRef::Flush] lines cause the reader to stop producing lines forever,
/// leaving [`Read`][std::io::Read] at the start of whatever comes next.
///
/// This implementation tries hard not to allocate at all which leads to quite some added complexity and plenty of extra memory copies.
pub struct StreamingPeekableIter<T> {
read: T,
peek_buf: Vec<u8>,
#[cfg(any(feature = "blocking-io", feature = "async-io"))]
buf: Vec<u8>,
fail_on_err_lines: bool,
delimiters: &'static [PacketLineRef<'static>],
is_done: bool,
stopped_at: Option<PacketLineRef<'static>>,
}

impl<T> StreamingPeekableIter<T> {
/// Return a new instance from `read` which will stop decoding packet lines when receiving one of the given `delimiters`.
pub fn new(read: T, delimiters: &'static [PacketLineRef<'static>]) -> Self {
Expand Down
10 changes: 4 additions & 6 deletions git-packetline/src/write/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::Writer;

#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
mod async_io;
#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
pub use async_io::Writer;
pub(crate) mod async_io;

#[cfg(feature = "blocking-io")]
mod blocking_io;
#[cfg(feature = "blocking-io")]
pub use blocking_io::Writer;
pub(crate) mod blocking_io;

/// Common methods
impl<T> Writer<T> {
Expand Down

0 comments on commit 0c7c599

Please sign in to comment.