Skip to content

beryl_heirloom

Typed ETS tables, heirs, and ownership transfers.

pub type Access {
Private
Protected
Public
}

The ETS table access level.

pub type CreateError {
TableAlreadyExists
InvalidName
InvalidHeir
InvalidOptions
}

An error returned while creating a table.

pub type Heir(a) {
NoHeir
Heir(
process: process.Pid,
data: a
)
}

The process that inherits a table when its owner exits.

pub type OwnershipError {
OwnershipTableDoesNotExist
NotOwner
InvalidOwnershipHeir
RecipientNotAlive
RecipientNotLocal
RecipientIsOwner
}

An error returned while changing table ownership.

pub type Spec(a)

Options used to create a table.

pub type Table(a, b)

An ETS table whose key and value types are tracked by Gleam.

ETS does not validate these types at runtime.

pub type TableError {
TableDoesNotExist
AccessDenied
}

An error returned while accessing a table.

pub type TableType {
Set
OrderedSet
Bag
DuplicateBag
}

The ETS table type.

pub type Transfer(a, b, c) {
Transfer(
table: Table(a, b),
previous_owner: process.Pid,
data: c
)
}

An ETS ownership transfer.

pub type TransferDecodeError {
InvalidTransferMessage
InvalidTable
InvalidPreviousOwner
InvalidHeirData(List(decode.DecodeError))
}

An error returned while decoding an ETS-TRANSFER message.

pub fn clear_heir(Table(a, b)) -> Result(Nil, OwnershipError)

Clear the table heir.

The caller must own the table.

pub fn create(Spec(a)) -> Result(Table(b, c), CreateError)

Create an ETS table owned by the calling process.

Invalid UTF-8 or names longer than 255 Unicode code points return InvalidName, for both named and unnamed tables.

Unnamed tables use the fixed atom beryl_heirloom for native ets:info(Table, name) metadata, not the configured label. The returned handle identifies the table instance.

pub fn delete(Table(a, b)) -> Result(Nil, TableError)

Delete a table owned by the calling process.

pub fn exists(Table(a, b)) -> Bool

Return whether a table currently exists.

pub fn give_away(
Table(a, b),
to: process.Pid,
data: c
) -> Result(Nil, OwnershipError)

Give the table to another live local process.

The caller must own the table. The recipient observes completion through an ETS-TRANSFER message. The configured heir is unchanged. If the recipient exits before ETS transfers ownership, this returns RecipientNotAlive and the caller keeps the table.

pub fn heir(Table(a, b)) -> Result(option.Option(process.Pid), TableError)

Return the configured live heir, if present.

pub fn insert(
Table(a, b),
a,
b
) -> Result(Nil, TableError)

Insert or replace a key and value.

pub fn lookup(
Table(a, b),
a
) -> Result(option.Option(b), TableError)

Look up one value for a key.

Set tables contain at most one value. Bag tables return the first value reported by ETS.

pub fn owner(Table(a, b)) -> Result(process.Pid, TableError)

Return the current table owner.

pub fn select_transfers(
process.Selector(a),
decode.Decoder(b),
fn(Result(Transfer(c, d, b), TransferDecodeError)) -> a
) -> process.Selector(a)

Add typed ETS ownership transfers to a process selector.

Transfers produced by this module carry a private native gift envelope containing the original table identity. The data decoder receives only the original gift. A deleted original table returns InvalidTable, even if its name has been reused.

Untagged native transfers carrying a live TID remain supported. Untagged name-only transfers return InvalidTable: a name cannot prove which table instance sent the message. The private envelope tag is reserved.

pub fn set_heir(
Table(a, b),
process.Pid,
c
) -> Result(Nil, OwnershipError)

Set the table heir.

The caller must own the table. The heir must be a live local process.

pub fn spec(
String,
TableType
) -> Spec(Nil)

Create a table specification with protected access and no heir.

Tables are unnamed by default. Creation validates the label without converting it to an atom.

pub fn with_access(
Spec(a),
Access
) -> Spec(a)

Set the table access level.

pub fn with_heir(
Spec(a),
process.Pid,
b
) -> Spec(b)

Configure a live local process as the table heir.

pub fn with_named_table(Spec(a)) -> Spec(a)

Register the table under its configured name.

Creation converts the name to a permanent VM atom. Use a fixed, finite set of names; do not derive names from requests or job identifiers.

pub fn with_read_concurrency(
Spec(a),
Bool
) -> Spec(a)

Enable or disable ETS read concurrency optimization.

pub fn with_write_concurrency(
Spec(a),
Bool
) -> Spec(a)

Enable or disable ETS write concurrency optimization.

pub fn without_heir(Spec(a)) -> Spec(Nil)

Remove the configured heir.