Packages

Mutable references that can be concurrently accessed, based on ETS tables

Current section

Files

Jump to
cell src cell.erl
Raw

src/cell.erl

-module(cell).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/cell.gleam").
-export([new_table/0, new/1, read/1, write/2, delete/1, drop/1]).
-export_type([table/0, cell_reference/1, cell/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type table() :: any().
-type cell_reference(DQD) :: any() | {gleam_phantom, DQD}.
-opaque cell(DQE) :: {cell, table(), cell_reference(DQE)}.
-file("src/cell.gleam", 20).
?DOC(
" Create a new table.\n"
"\n"
" The process that calls this function is the owner of the ETS table, and the\n"
" table will be dropped when the owner process terminates.\n"
).
-spec new_table() -> table().
new_table() ->
cell_ffi:new_table().
-file("src/cell.gleam", 30).
?DOC(
" Create a new cell.\n"
"\n"
" If the table the cell is part of to is dropped then the cell will no longer\n"
" be usable.\n"
).
-spec new(table()) -> cell(any()).
new(Table) ->
{cell, Table, erlang:make_ref()}.
-file("src/cell.gleam", 39).
?DOC(
" Read the current value contained by a cell.\n"
"\n"
" This function will return an error if the table has been dropped, or if the\n"
" cell does not yet have a value.\n"
).
-spec read(cell(DQJ)) -> {ok, DQJ} | {error, nil}.
read(Cell) ->
cell_ffi:read(erlang:element(2, Cell), erlang:element(3, Cell)).
-file("src/cell.gleam", 50).
?DOC(
" Set the value of a cell, overwriting any previous value.\n"
"\n"
" This function will return an error if the table has been dropped.\n"
).
-spec write(cell(DQR), DQR) -> {ok, nil} | {error, nil}.
write(Cell, Value) ->
cell_ffi:write(erlang:element(2, Cell), erlang:element(3, Cell), Value).
-file("src/cell.gleam", 59).
?DOC(" Delete the value from a cell, freeing the memory used.\n").
-spec delete(cell(any())) -> {ok, nil} | {error, nil}.
delete(Cell) ->
cell_ffi:empty(erlang:element(2, Cell), erlang:element(3, Cell)).
-file("src/cell.gleam", 71).
?DOC(
" Drop a table, freeing the memory it used.\n"
"\n"
" Any attempt to use a table after it was dropped will result in an error.\n"
).
-spec drop(table()) -> nil.
drop(Table) ->
cell_ffi:drop(Table).