rasa/table

Type-safe key-value tables backed by Erlang’s ETS. Tables are configured using a builder pattern, where you specify the table type (Set or OrderedSet) and access level (Public, Protected, or Private) before creating the table.

Requires OTP 27 or later.

Types

The access level of an ETS table. Public allows any process to read and write. Protected allows any process to read but only the owner to write. Private restricts both reads and writes to the owning process.

pub type Access {
  Public
  Protected
  Private
}

Constructors

  • Public
  • Protected
  • Private

A Table builder. Defaults to Set and Protected, matching the defaults specified by the erlang ets module.

pub opaque type Builder

The type of ETS table. Set tables have unordered keys. OrderedSet tables maintain keys in sorted order.

pub type Kind {
  Set
  OrderedSet
}

Constructors

  • Set
  • OrderedSet

An ETS table for storing key/value pairs

pub opaque type Table(a, b)

Values

pub fn access(table: Table(a, b)) -> Result(Access, Nil)

Returns the table’s Access level (Public, Protected, or Private).

pub fn build(builder: Builder) -> Table(a, b)

Returns a new ETS table configured according to the provided Builder.

pub fn delete(table: Table(a, b), key: a) -> Result(Nil, Nil)

Removes a key and its value from the table. Succeeds even if the key does not exist in the table.

pub fn drop(table: Table(a, b)) -> Result(Nil, Nil)

Deletes the table.

pub fn first(table: Table(a, b)) -> Result(#(a, b), Nil)

Returns the first key-value pair in the table without removing it from the table. The order is guaranteed only for OrderedSet tables. See the ets docs for more details.

pub fn insert(
  table: Table(a, b),
  key: a,
  value: b,
) -> Result(Nil, Nil)

Inserts a key and value into the table. If the key already exists, its value is replaced with the new one.

pub fn insert_new(
  table: Table(a, b),
  key: a,
  value: b,
) -> Result(Nil, Nil)

Inserts a key and value into the table only if the key does not already exist. Returns Error(Nil) if the key is already present.

pub fn is_empty(table: Table(a, b)) -> Bool

Determines whether or not the table is empty. Returns True if the table does not exist.

pub fn kind(table: Table(a, b)) -> Result(Kind, Nil)

Returns the table’s Kind (Set or OrderedSet).

pub fn last(table: Table(a, b)) -> Result(#(a, b), Nil)

Returns the last key-value pair in the table without removing it from the table. The order is guaranteed only for OrderedSet tables. See the ets docs for more details.

pub fn lookup(table: Table(a, b), key: a) -> Result(b, Nil)

Returns the value associated with the given key, or Error(Nil) if the key does not exist.

pub fn new() -> Builder

Returns a Builder. Defaults to Set and Protected.

pub fn size(table: Table(a, b)) -> Result(Int, Nil)

Returns the size of the table.

pub fn to_list(table: Table(a, b)) -> Result(List(#(a, b)), Nil)

Returns all entries in the table as a list of key-value tuples. The order is guaranteed only for OrderedSet tables.

pub fn with_access(builder: Builder, access: Access) -> Builder

Sets the access level on the builder.

pub fn with_kind(builder: Builder, kind: Kind) -> Builder

Sets the table type on the builder.

Search Document