pub struct IoManagerBuilder<SinkType, StreamType, BF = fn(<StreamType as Stream>::Item, &mut IoWriter<<SinkType as Sink>::SinkItem>) -> Option<<StreamType as Stream>::Item>, BEH = fn(<StreamType as Stream>::Error)>where
SinkType: Sink,
StreamType: Stream,
BF: FnMut(<StreamType as Stream>::Item, &mut IoWriter<<SinkType as Sink>::SinkItem>) -> Option<<StreamType as Stream>::Item> + Send + 'static,
BEH: FnMut(<StreamType as Stream>::Error) + Send + 'static,{ /* private fields */ }
Expand description
A builder for IoManager
, and the only way to build one since the constructors have been deleted.
Implementations§
Source§impl<SinkType, StreamType> IoManagerBuilder<SinkType, StreamType>
impl<SinkType, StreamType> IoManagerBuilder<SinkType, StreamType>
Sourcepub fn new(
sink: SinkType,
stream: StreamType,
) -> IoManagerBuilder<SinkType, StreamType, fn(<StreamType as Stream>::Item, &mut IoWriter<<SinkType as Sink>::SinkItem>) -> Option<<StreamType as Stream>::Item>, fn(<StreamType as Stream>::Error)>
pub fn new( sink: SinkType, stream: StreamType, ) -> IoManagerBuilder<SinkType, StreamType, fn(<StreamType as Stream>::Item, &mut IoWriter<<SinkType as Sink>::SinkItem>) -> Option<<StreamType as Stream>::Item>, fn(<StreamType as Stream>::Error)>
Creates a builder for IoManager
.
Examples found in repository?
examples/tcp_server.rs (line 54)
51fn process_socket(socket: TcpStream) {
52 println!("New Client");
53 let (sink, stream) = LineCodec.framed(socket).split();
54 let trx = IoManagerBuilder::new(sink, stream)
55 .with_filter(|frame, writer| {
56 if frame.to_lowercase().contains("hello there") {
57 writer.write("General Kenobi!".to_string());
58 return None;
59 }
60 Some(frame)
61 })
62 .with_error_handler(move |error| {
63 println!("{}", error);
64 })
65 .build();
66 let mut writer = trx.get_writer();
67 trx.on_receive(move |frame| {
68 println!("Got frame: {}", frame);
69 writer.write("Hi there".to_string());
70 Ok(())
71 });
72}
Source§impl<SinkType, StreamType, FilterType, ErrorHandlerType> IoManagerBuilder<SinkType, StreamType, FilterType, ErrorHandlerType>where
SinkType: Sink + Send + 'static,
StreamType: Stream + Send + 'static,
<StreamType as Stream>::Item: Send + Clone + 'static,
<StreamType as Stream>::Error: Send,
<SinkType as Sink>::SinkItem: Send + 'static,
FilterType: Filter<<SinkType as Sink>::SinkItem, <StreamType as Stream>::Item>,
ErrorHandlerType: ErrorHandler<<StreamType as Stream>::Error>,
impl<SinkType, StreamType, FilterType, ErrorHandlerType> IoManagerBuilder<SinkType, StreamType, FilterType, ErrorHandlerType>where
SinkType: Sink + Send + 'static,
StreamType: Stream + Send + 'static,
<StreamType as Stream>::Item: Send + Clone + 'static,
<StreamType as Stream>::Error: Send,
<SinkType as Sink>::SinkItem: Send + 'static,
FilterType: Filter<<SinkType as Sink>::SinkItem, <StreamType as Stream>::Item>,
ErrorHandlerType: ErrorHandler<<StreamType as Stream>::Error>,
Sourcepub fn with_filter<NewFilterType>(
self,
filter: NewFilterType,
) -> IoManagerBuilder<SinkType, StreamType, NewFilterType, ErrorHandlerType>
pub fn with_filter<NewFilterType>( self, filter: NewFilterType, ) -> IoManagerBuilder<SinkType, StreamType, NewFilterType, ErrorHandlerType>
Adds a filter to the IoManager
builder.
Filters are static in this library. If you need to be able to change the filter without
droping the sink and steram passed to this instance, you should probably use Box to encapsulate your filter,
and then whatever you need to make it all thread safe for when you’ll need to modify it.
Type inference should still work, which is nice.
Examples found in repository?
examples/tcp_server.rs (lines 55-61)
51fn process_socket(socket: TcpStream) {
52 println!("New Client");
53 let (sink, stream) = LineCodec.framed(socket).split();
54 let trx = IoManagerBuilder::new(sink, stream)
55 .with_filter(|frame, writer| {
56 if frame.to_lowercase().contains("hello there") {
57 writer.write("General Kenobi!".to_string());
58 return None;
59 }
60 Some(frame)
61 })
62 .with_error_handler(move |error| {
63 println!("{}", error);
64 })
65 .build();
66 let mut writer = trx.get_writer();
67 trx.on_receive(move |frame| {
68 println!("Got frame: {}", frame);
69 writer.write("Hi there".to_string());
70 Ok(())
71 });
72}
Sourcepub fn with_error_handler<NewErrorHandlerType>(
self,
handler: NewErrorHandlerType,
) -> IoManagerBuilder<SinkType, StreamType, FilterType, NewErrorHandlerType>
pub fn with_error_handler<NewErrorHandlerType>( self, handler: NewErrorHandlerType, ) -> IoManagerBuilder<SinkType, StreamType, FilterType, NewErrorHandlerType>
Similar to with_filter
, only for error handling.
Tip: if you want to be able to catch end of streams with this API,
you may want your Codec to implement decode_eof()
and throw an error at the last moment,
such as this:
fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let decode = self.decode(buf);
match decode {
Ok(None) => Err(std::io::Error::from(std::io::ErrorKind::ConnectionAborted)),
_ => decode,
}
}
Examples found in repository?
examples/tcp_server.rs (lines 62-64)
51fn process_socket(socket: TcpStream) {
52 println!("New Client");
53 let (sink, stream) = LineCodec.framed(socket).split();
54 let trx = IoManagerBuilder::new(sink, stream)
55 .with_filter(|frame, writer| {
56 if frame.to_lowercase().contains("hello there") {
57 writer.write("General Kenobi!".to_string());
58 return None;
59 }
60 Some(frame)
61 })
62 .with_error_handler(move |error| {
63 println!("{}", error);
64 })
65 .build();
66 let mut writer = trx.get_writer();
67 trx.on_receive(move |frame| {
68 println!("Got frame: {}", frame);
69 writer.write("Hi there".to_string());
70 Ok(())
71 });
72}
Sourcepub fn build(self) -> IoManager<SinkType::SinkItem, StreamType::Item>
pub fn build(self) -> IoManager<SinkType::SinkItem, StreamType::Item>
Examples found in repository?
examples/tcp_server.rs (line 65)
51fn process_socket(socket: TcpStream) {
52 println!("New Client");
53 let (sink, stream) = LineCodec.framed(socket).split();
54 let trx = IoManagerBuilder::new(sink, stream)
55 .with_filter(|frame, writer| {
56 if frame.to_lowercase().contains("hello there") {
57 writer.write("General Kenobi!".to_string());
58 return None;
59 }
60 Some(frame)
61 })
62 .with_error_handler(move |error| {
63 println!("{}", error);
64 })
65 .build();
66 let mut writer = trx.get_writer();
67 trx.on_receive(move |frame| {
68 println!("Got frame: {}", frame);
69 writer.write("Hi there".to_string());
70 Ok(())
71 });
72}
Auto Trait Implementations§
impl<SinkType, StreamType, BF, BEH> Freeze for IoManagerBuilder<SinkType, StreamType, BF, BEH>
impl<SinkType, StreamType, BF, BEH> RefUnwindSafe for IoManagerBuilder<SinkType, StreamType, BF, BEH>
impl<SinkType, StreamType, BF, BEH> Send for IoManagerBuilder<SinkType, StreamType, BF, BEH>
impl<SinkType, StreamType, BF, BEH> Sync for IoManagerBuilder<SinkType, StreamType, BF, BEH>
impl<SinkType, StreamType, BF, BEH> Unpin for IoManagerBuilder<SinkType, StreamType, BF, BEH>
impl<SinkType, StreamType, BF, BEH> UnwindSafe for IoManagerBuilder<SinkType, StreamType, BF, BEH>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more