|
1 | | -import gleam/string |
2 | | -import gleam/dynamic.{type Dynamic} |
| 1 | +import gleam/dynamic.{type DecodeErrors, type Dynamic} |
3 | 2 | import gleam/erlang.{type Reference} |
4 | 3 | import gleam/erlang/atom.{type Atom} |
| 4 | +import gleam/string |
5 | 5 |
|
6 | 6 | /// A `Pid` (or Process identifier) is a reference to an Erlang process. Each |
7 | 7 | /// process has a `Pid` and it is one of the lowest level building blocks of |
@@ -563,10 +563,9 @@ pub fn try_call( |
563 | 563 | let result = |
564 | 564 | new_selector() |
565 | 565 | |> selecting(reply_subject, Ok) |
566 | | - |> selecting_process_down( |
567 | | - monitor, |
568 | | - fn(down: ProcessDown) { Error(CalleeDown(reason: down.reason)) }, |
569 | | - ) |
| 566 | + |> selecting_process_down(monitor, fn(down: ProcessDown) { |
| 567 | + Error(CalleeDown(reason: down.reason)) |
| 568 | + }) |
570 | 569 | |> select(timeout) |
571 | 570 |
|
572 | 571 | // Demonitor the process and close the channels as we're done |
@@ -744,3 +743,18 @@ pub fn unregister(name: Atom) -> Result(Nil, Nil) |
744 | 743 | /// |
745 | 744 | @external(erlang, "gleam_erlang_ffi", "process_named") |
746 | 745 | pub fn named(name: Atom) -> Result(Pid, Nil) |
| 746 | + |
| 747 | +/// Checks to see whether a `Dynamic` value is a pid, and return the pid if |
| 748 | +/// it is. |
| 749 | +/// |
| 750 | +/// ## Examples |
| 751 | +/// |
| 752 | +/// > import gleam/dynamic |
| 753 | +/// > from_dynamic(dynamic.from(process.self())) |
| 754 | +/// Ok(process.self()) |
| 755 | +/// |
| 756 | +/// > from_dynamic(dynamic.from(123)) |
| 757 | +/// Error([DecodeError(expected: "Pid", found: "Int", path: [])]) |
| 758 | +/// |
| 759 | +@external(erlang, "gleam_erlang_ffi", "pid_from_dynamic") |
| 760 | +pub fn pid_from_dynamic(from from: Dynamic) -> Result(Pid, DecodeErrors) |
0 commit comments