Struct RMonitorDecoder

Source
pub struct RMonitorDecoder { /* private fields */ }
Expand description

A decoder for RMonitor records, which wraps an underlying LinesCodec to provide framing logic.

Implementations§

Source§

impl RMonitorDecoder

Source

pub fn new() -> Self

Returns an RMonitorDecoder for decoding RMonitor records from a TCP stream.

§Note

The returned RMonitorDecoder will have an underlying LinesCodec with no upper bound on the length of a buffered line. Consider using new_with_max_length instead.

Source

pub fn new_with_max_length(max_length: usize) -> Self

Returns an RMonitorDecoder where the underlying LinesCodec has a maximum line length limit.

It is recommended to set such a limit where the input to be supplied to the decoder is untrusted, as an attacker could send an unbounded amount of input with no newline characters.

Examples found in repository?
examples/simple.rs (line 13)
10async fn main() -> Result<(), Box<dyn Error>> {
11    let stream = TcpStream::connect("127.0.0.1:50000").await?;
12
13    let mut reader = FramedRead::new(stream, RMonitorDecoder::new_with_max_length(2048));
14
15    while let Ok(Some(Ok(event))) = timeout(Duration::from_secs(5), reader.next()).await {
16        println!("{:?}", event);
17    }
18
19    Ok(())
20}
More examples
Hide additional examples
examples/sync.rs (line 17)
7fn main() {
8    let mut stream = TcpStream::connect("127.0.0.1:4000").expect("Failed to open connection");
9
10    // .read() won't use the BytesMut directly in the appropriate manner,
11    // so we do one extra bit of buffering here
12    let mut read_buf = [0u8; 256];
13
14    // Create the decode buffer, and a decoder with a maximum line length
15    // of 2048.
16    let mut buffer = BytesMut::with_capacity(4096);
17    let mut decoder = RMonitorDecoder::new_with_max_length(2048);
18
19    loop {
20        let r = stream
21            .read(&mut read_buf)
22            .expect("Failed to read from stream");
23
24        // Put only the read bytes into the decode buffer (not any trailing 0s)
25        if r > 0 {
26            buffer.put(&read_buf[..r]);
27        }
28
29        let maybe_record = decoder.decode(&mut buffer);
30
31        match maybe_record {
32            Ok(None) => {
33                continue;
34            }
35            Ok(Some(r)) => println!("{:?}", r),
36            Err(e) => println!("{:?}", e),
37        }
38    }
39}

Trait Implementations§

Source§

impl Debug for RMonitorDecoder

Source§

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

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

impl Decoder for RMonitorDecoder

Source§

type Item = Record

The type of decoded frames.
Source§

type Error = RMonitorCodecError

The type of unrecoverable frame decoding errors. Read more
Source§

fn decode( &mut self, src: &mut BytesMut, ) -> Result<Option<Self::Item>, Self::Error>

Attempts to decode a frame from the provided buffer of bytes. Read more
Source§

fn decode_eof( &mut self, buf: &mut BytesMut, ) -> Result<Option<Self::Item>, Self::Error>

A default method available to be called when there are no more bytes available to be read from the underlying I/O. Read more
Source§

fn framed<T>(self, io: T) -> Framed<T, Self>
where T: AsyncRead + AsyncWrite, Self: Sized,

Provides a Stream and Sink interface for reading and writing to this Io object, using Decode and Encode to read and write the raw data. Read more
Source§

impl Default for RMonitorDecoder

Source§

fn default() -> RMonitorDecoder

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

Auto Trait Implementations§

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> 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, 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.