Created
November 27, 2025 17:00
-
-
Save dagi3d/1b0dc20b225f0fd89db22317f4bcaff8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use rustler::{Env, LocalPid}; | |
| mod atoms { | |
| rustler::atoms! { | |
| foo, | |
| bar, | |
| foobar | |
| } | |
| } | |
| #[rustler::nif] | |
| fn send_message<'a>(env: Env<'a>, pid: LocalPid) -> () { | |
| let _ = env.send(&pid, (atoms::foo(), "hello from rust - foo")); | |
| let _ = env.send(&pid, (atoms::bar(), "hello from rust - bar")); | |
| let _ = env.send( | |
| &pid, | |
| (atoms::bar(), "hello from rust - bar", "another param"), | |
| ); | |
| let _ = env.send(&pid, "hello from rust - foobar"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import gleam/dynamic.{type Dynamic} | |
| import gleam/erlang/atom | |
| import gleam/erlang/process | |
| import gleam/io | |
| import gleam/otp/actor | |
| import gleam/string | |
| @external(erlang, "librs", "send_message") | |
| pub fn send_message(pid: process.Pid) -> Nil | |
| pub type Message { | |
| Message(Dynamic) | |
| OtherMessage(Dynamic) | |
| } | |
| fn handle_message(state, message: Message) -> actor.Next(List(Int), Message) { | |
| case message { | |
| value -> io.println("message received: " <> string.inspect(value)) | |
| } | |
| actor.continue(state) | |
| } | |
| pub fn main() -> Nil { | |
| let selector = | |
| process.new_selector() | |
| |> process.select_record(atom.create("foo"), 1, fn(value) { Message(value) }) | |
| |> process.select_record(atom.create("bar"), 2, fn(value) { Message(value) }) | |
| |> process.select_other(OtherMessage) | |
| let assert Ok(actor) = | |
| actor.new_with_initialiser(1000, fn(_) { | |
| actor.initialised([]) | |
| |> actor.selecting(selector) | |
| |> Ok | |
| }) | |
| |> actor.on_message(handle_message) | |
| |> actor.start | |
| send_message(actor.pid) | |
| process.sleep_forever() | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Receive messages from a rust program