Crate erlang_port[][src]

erlang_port helps make writing Erlang/Elixir ports in Rust easier.

Makes use of the serde_eetf crate to serialize/deserialize rust datatypes into erlang external term format, suitable for passing to/receiving from binary_to_term/term_to_binary

Assuming you are starting your port in packet mode, it's recommended that you use the stdio or nouse_stdio functions inside the main fuction of your application to create an IOPort. You can then use the sender and receiver properties on that IOPort to communicate with Erlang/Elixir.

For example, if you create the following rust program that reads strings from a port and returns them uppercased:

fn lower(mut s: String) -> Result<String, String> {
    s.make_ascii_uppercase();
    Ok(s)
}

fn main() {
   use erlang_port::{PortReceive, PortSend};

  let mut port = unsafe {
      use erlang_port::PacketSize;
      erlang_port::nouse_stdio(PacketSize::Four)
  };

  for string_in in port.receiver.iter() {
      let result = lower(string_in);

      port.sender.reply(result);
  }
}

Then you can call into this port from Elixir:

iex> port =
...>   Port.open({:spawn_executable, port_path}, [
...>       {:packet, 4},
...>       :nouse_stdio,
...>       :binary,
...>       :exit_status
...>   ])
#Port<0.1444>
iex> Port.command(port, :erlang.term_to_binary("hello"))
true
iex> receive do
...>   {^port, {:data, binary}} ->
...>     IO.puts(:erlang.binary_to_term(binary))
...> end
"HELLO"
:ok

If you wish to implement a line-based port or a custom port protocol (using the :stream option) you can do so by implementing the PortSend/PortReceive traits.

Structs

IOPort

A wrapper around a receiver and a sender.

MessageIterator

Iterator over messages from a PortReceive.

PacketReceiver

A receiver for ports opened in Packet mode.

PacketSender

A sender for ports opened in Packet mode.

Enums

ErlResult

A result enum for replying to commands from Erlang.

PacketSize

The size of a packet as sent/received by a PacketReceiver or PacketSender

Traits

MessageDeserialize

Trait that parses some data from a Vec

MessageSerialize

Trait that serializes some data into a Vec

PortReceive

PortReceive allows an app to receive messages through an Erlang port.

PortSend

PortSend allows an application to send messages through an Erlang port.

Functions

nouse_stdio

Creates an IOPort using file descriptors 3 and 4 with the specified packet size.

stdio

Creates an IOPort using stdin & stdout with the specified packet size.