tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

57 lignes · 1714 octetsBrut · Historique
1/// Tables any actor reads without a message (ADR 0073), like Erlang's ETS
2/// with `read_concurrency` or `persistent_term`: a request reads the latest
3/// value under a key while one actor keeps it current.
4///
5/// let latest = table.new ()
6/// table.put latest "ada" notebook // from the actor that owns it
7/// table.get latest "ada" // from any actor: Some(notebook)
8///
9/// A stored value is shared, never copied: `put` makes it shareable once,
10/// and every `get` hands out the same value. A reader keeps what it read
11/// after a later `put` or `remove`. Tables live as long as the program;
12/// replaced and removed values are released when their last reader is
13/// done with them.
14
15import std/process.{type proc}
16
17pub opaque type Table<v> { Table(Int) }
18
19/// A new, empty table.
20pub fn new :: -> Table<v> / <proc>
21{
22 Table(new_raw ())
23}
24
25/// The value under `key`, if any.
26pub fn get :: table: Table<v> key: String -> Option<v> / <proc>
27{
28 let Table(id) = table
29 get_raw id key
30}
31
32/// Store `value` under `key`, replacing what was there. Only sendable
33/// values go in: every actor may read them.
34pub fn put :: table: Table<v> key: String value: v -> () / <proc> where Send<v>
35{
36 let Table(id) = table
37 put_raw id key value
38}
39
40/// Remove `key`, if present.
41pub fn remove :: table: Table<v> key: String -> () / <proc>
42{
43 let Table(id) = table
44 remove_raw id key
45}
46
47@extern c "tl_table_new"
48fn new_raw :: -> Int / <proc>
49
50@extern c "tl_table_get"
51fn get_raw :: id: Int key: String -> Option<v> / <proc>
52
53@extern c "tl_table_put" owned
54fn put_raw :: id: Int key: String value: v -> () / <proc>
55
56@extern c "tl_table_remove"
57fn remove_raw :: id: Int key: String -> () / <proc>