pub fn write_to_stream<W: Write, T: Serialize>
    (stream : &mut W, obj : &T) -> Result<(), Error> {
    let buf = match serde_json::to_vec(obj) {
        Ok(b)   => b,
        Err(_)  => return Err(Error::SerializationError)
    };

    let len = buf.len();

    let len_arr = match u16::try_from(len) {
        Ok(l)   => l.to_be_bytes(),
        Err(_)  => return Err(Error::PayloadTooBig)
    };

    if let Err(_) = stream.write(&len_arr) {
        return Err(Error::NetworkWriteError)
    }
    if let Err(_) = stream.write(&buf) {
        return Err(Error::NetworkWriteError)
    }

    Ok(())
}
/// This function writes a struct that implements the `Serialize` trait to an output stream
/// The output stream must implement the `std::io::Write` trait
///
/// Returns a `Result` to simply indicate if the write succeeded
///
/// # Arguments
///
/// * `stream` - The input stream
/// * `obj` - The instance of the struct to write
This function writes a struct that implements the `Serialize` trait to an output stream
The output stream must implement the `std::io::Write` trait

Returns a `Result` to simply indicate if the write succeeded
This function writes a struct that implements the ` Serialize ` trait to an output stream The output stream must implement the ` std :: io :: Write ` trait Returns a ` Result ` to simply indicate if the write succeeded
pub fn write_to_stream<W: Write, T: Serialize>
    (stream : &mut W, obj : &T) -> Result<(), Error> {
    let buf = match serde_json::to_vec(obj) {
        Ok(b)   => b,
        Err(_)  => return Err(Error::SerializationError)
    };
    let len = buf.len();
    let len_arr = match u16::try_from(len) {
        Ok(l)   => l.to_be_bytes(),
        Err(_)  => return Err(Error::PayloadTooBig)
    };
    if let Err(_) = stream.write(&len_arr) {
        return Err(Error::NetworkWriteError)
    }
    if let Err(_) = stream.write(&buf) {
        return Err(Error::NetworkWriteError)
    }
    Ok(())
}
pub fn write_to_stream < W : Write , T : Serialize > ( stream : & mut W , obj : & T ) -> Result < ( ) , Error > { let buf = match serde_json :: to_vec ( obj ) { Ok ( b ) => b , Err ( _ ) => return Err ( Error :: SerializationError ) } ; let len = buf . len ( ) ; let len_arr = match u16 :: try_from ( len ) { Ok ( l ) => l . to_be_bytes ( ) , Err ( _ ) => return Err ( Error :: PayloadTooBig ) } ; if let Err ( _ ) = stream . write ( & len_arr ) { return Err ( Error :: NetworkWriteError ) } if let Err ( _ ) = stream . write ( & buf ) { return Err ( Error :: NetworkWriteError ) } Ok ( ( ) ) }
This function writes a struct that implements the `Serialize` trait to an output stream
The output stream must implement the `std::io::Write` trait
This function writes a struct that implements the ` Serialize ` trait to an output stream The output stream must implement the ` std :: io :: Write ` trait
stream
&mut W
obj
&T
stream
&mut W
obj
&T