Skip to content

Commit e6b2bc7

Browse files
committed
Merge pull request #4707 from whatyouhide/genserver-and-supervisor-behaviours
Add GenServer.format_status/2 and use GenServer over :gen_server as the behaviour
2 parents 14f17f8 + d0879c4 commit e6b2bc7

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/elixir/lib/gen_server.ex

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,28 @@ defmodule GenServer do
382382
{:ok, new_state :: term} |
383383
{:error, reason :: term} when old_vsn: term | {:down, term}
384384

385+
@doc """
386+
Invoked in some cases to retrieve a formatted version of the `GenServer` status.
387+
388+
This callback can be useful to control the *appearance* of the status of the
389+
`GenServer`. For example, it can be used to return a compact representation of
390+
the `GenServers`'s state to avoid having large state terms printed.
391+
392+
* one of `:sys.get_status/1` or `:sys.get_status/2` is invoked to get the
393+
status of the `GenServer`; in such cases, `reason` is `:normal`
394+
395+
* the `GenServer` terminates abnormally and logs an error; in such cases,
396+
`reason` is `:terminate`
397+
398+
`pdict_and_state` is a two-elements list `[pdict, state]` where `pdict` is a
399+
list of `{key, value}` tuples representing the current process dictionary of
400+
the `GenServer` and `state` is the current state of the `GenServer`.
401+
"""
402+
@callback format_status(reason, pdict_and_state :: list) ::
403+
term when reason: :normal | :terminate
404+
405+
@optional_callbacks format_status: 2
406+
385407
@typedoc "Return values of `start*` functions"
386408
@type on_start :: {:ok, pid} | :ignore | {:error, {:already_started, pid} | term}
387409

@@ -414,7 +436,7 @@ defmodule GenServer do
414436
@doc false
415437
defmacro __using__(_) do
416438
quote location: :keep do
417-
@behaviour :gen_server
439+
@behaviour GenServer
418440

419441
@doc false
420442
def init(args) do

lib/elixir/test/elixir/module_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ defmodule ModuleTest do
122122
## Attributes
123123

124124
test "reserved attributes" do
125-
assert List.keyfind(ExUnit.Server.__info__(:attributes), :behaviour, 0) == {:behaviour, [:gen_server]}
125+
assert List.keyfind(ExUnit.Server.__info__(:attributes), :behaviour, 0) == {:behaviour, [GenServer]}
126126
end
127127

128128
test "registered attributes" do

0 commit comments

Comments
 (0)