Struct Connection

Source
pub struct Connection<R: BufRead, W: Write> { /* private fields */ }
Expand description

Use this type when you have both a reader and writer, and want them to be grouped together.

There are situations in which you have both a reader and a writer being passed around code, always kept together. This forms what is known as a ‘data clump’, and harms code readability. By grouping the two together it makes clear that they are both needed, and prevents mistakes when one is forgotten.

Connection is internally a pair of a reader and a writer, and delegates to crate::read and crate::write for Connection::read and Connection::write respectively.

Implementations§

Source§

impl<R: BufRead, W: Write> Connection<R, W>

Source

pub fn new(reader: R, writer: W) -> Self

Source§

impl<'a> Connection<BufReader<&'a mut ChildStdout>, &'a mut ChildStdin>

Source

pub fn new_from_child(child: &'a mut Child) -> Option<Self>

Creates a new Connection that uses the stdin of a child process as the writer and the child process’ stdout as the reader. This facilitates communication with this child process by passing data into its stdin and reading from its stdout.

Source§

impl Connection<BufReader<Stdin>, Stdout>

Source

pub fn new_from_stdio() -> Self

Creates a new Connection from the stdio of the current process – stdin is used as the reader and stdout is used as the writer.

Source§

impl Connection<BufReader<TcpStream>, TcpStream>

Source

pub fn new_from_tcp_stream(tcp_stream: TcpStream) -> Result<Self>

Creates a new Connection from a TCP stream.

Source

pub fn shutdown(self) -> Result<()>

Closes the TCP stream.

Source§

impl<R: BufRead, W: Write> Connection<R, W>

Source

pub fn read<T: DeserializeOwned>(&mut self) -> Result<T, ReadError>

Reads a line from the reader and deserializes it into a given type.

Source

pub fn write<T: Serialize>(&mut self, t: &T) -> Result<(), WriteError>

Writes a given value to the writer, serializing it into JSON.

Source

pub fn flush(&mut self) -> Result<()>

Flushes the contained writer’s buffer.

Trait Implementations§

Source§

impl<R: Clone + BufRead, W: Clone + Write> Clone for Connection<R, W>

Source§

fn clone(&self) -> Connection<R, W>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: Debug + BufRead, W: Debug + Write> Debug for Connection<R, W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R: Default + BufRead, W: Default + Write> Default for Connection<R, W>

Source§

fn default() -> Connection<R, W>

Returns the “default value” for a type. Read more
Source§

impl<R: Hash + BufRead, W: Hash + Write> Hash for Connection<R, W>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<R: Ord + BufRead, W: Ord + Write> Ord for Connection<R, W>

Source§

fn cmp(&self, other: &Connection<R, W>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<R: PartialEq + BufRead, W: PartialEq + Write> PartialEq for Connection<R, W>

Source§

fn eq(&self, other: &Connection<R, W>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<R: PartialOrd + BufRead, W: PartialOrd + Write> PartialOrd for Connection<R, W>

Source§

fn partial_cmp(&self, other: &Connection<R, W>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<R: Copy + BufRead, W: Copy + Write> Copy for Connection<R, W>

Source§

impl<R: Eq + BufRead, W: Eq + Write> Eq for Connection<R, W>

Source§

impl<R: BufRead, W: Write> StructuralPartialEq for Connection<R, W>

Auto Trait Implementations§

§

impl<R, W> Freeze for Connection<R, W>
where R: Freeze, W: Freeze,

§

impl<R, W> RefUnwindSafe for Connection<R, W>

§

impl<R, W> Send for Connection<R, W>
where R: Send, W: Send,

§

impl<R, W> Sync for Connection<R, W>
where R: Sync, W: Sync,

§

impl<R, W> Unpin for Connection<R, W>
where R: Unpin, W: Unpin,

§

impl<R, W> UnwindSafe for Connection<R, W>
where R: UnwindSafe, W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.