Trait erlang_port::PortSend [−][src]
pub trait PortSend { fn send<T>(&mut self, data: T)
where
T: MessageSerialize; fn reply<R, T, E>(&mut self, response: R)
where
R: Into<ErlResult<T, E>>,
T: Serialize,
E: Serialize, { ... } }
PortSend
allows an application to send messages through an Erlang port.
Required Methods
fn send<T>(&mut self, data: T) where
T: MessageSerialize,
T: MessageSerialize,
Sends a single message over the port.
A MessageSerialize
implementation shold exist for the message we are
reading. We provide a serde_eetf
based default implementation for any
type implementing serdes Serialize
.
In Erlang "Let it Crash" style, if we fail to encode the command or write it to a stream we will panic. Since this library is intended to be used as part of an Erlang system this should be picked up by the BEAM VM which can restart the Port.
It's possible that panicing is not what you want, for example if you have a Port that is handling multiple commands concurrently. Feel free to make a PR to better support your use case if that is the case.
Provided Methods
fn reply<R, T, E>(&mut self, response: R) where
R: Into<ErlResult<T, E>>,
T: Serialize,
E: Serialize,
R: Into<ErlResult<T, E>>,
T: Serialize,
E: Serialize,
Sends an {:ok, T}
or {:error, E}
over the port.
This can be used to reply to a command that was received in the port, or to send arbitrary commands to the Port inside the BEAM.
Applications can pass any data type they want into reply
, provided there's
a definition of Into<ErlResult<E, T>>
for that type. A default
implementation is provided for Result<E, T>
.
Note that both E and T must implement Serialize
rather than
MessageSerialize
as is the case for send
.
If you wish to return a custom type you can implement Into<ErlResult<T, E>>
for that type, or use the send
function instead to send a custom
type.
At the moment this function is just a simple wrapper around send
but that
may change in the future.
In Erlang "Let it Crash" style, if we fail to encode the command or write it to a stream we will panic. Since this library is intended to be used as part of an Erlang system this should be picked up by the BEAM VM which can restart the Port.
It's possible that panicing is not what you want, for example if you have a Port that is handling multiple commands concurrently. Feel free to make a PR to better support your use case if that is the case.
Implementors
impl<W> PortSend for PacketSender<W> where
W: Write,