lisette-stdlib 0.1.14

Little language inspired by Rust that compiles to Go
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Generated by Lisette bindgen
// Source: sync (Go stdlib)
// Go: 1.25.5
// Lisette: 0.1.14

pub fn NewCond(l: Locker) -> Ref<Cond>

/// OnceFunc returns a function that invokes f only once. The returned function
/// may be called concurrently.
/// 
/// If f panics, the returned function will panic with the same value on every call.
pub fn OnceFunc(f: fn() -> ()) -> fn() -> ()

/// OnceValue returns a function that invokes f only once and returns the value
/// returned by f. The returned function may be called concurrently.
/// 
/// If f panics, the returned function will panic with the same value on every call.
pub fn OnceValue<T>(f: fn() -> T) -> fn() -> T

/// OnceValues returns a function that invokes f only once and returns the values
/// returned by f. The returned function may be called concurrently.
/// 
/// If f panics, the returned function will panic with the same value on every call.
pub fn OnceValues<T1, T2>(f: fn() -> (T1, T2)) -> fn() -> (T1, T2)

/// Cond implements a condition variable, a rendezvous point
/// for goroutines waiting for or announcing the occurrence
/// of an event.
/// 
/// Each Cond has an associated Locker L (often a [*Mutex] or [*RWMutex]),
/// which must be held when changing the condition and
/// when calling the [Cond.Wait] method.
/// 
/// A Cond must not be copied after first use.
/// 
/// In the terminology of [the Go memory model], Cond arranges that
/// a call to [Cond.Broadcast] or [Cond.Signal] “synchronizes before” any Wait call
/// that it unblocks.
/// 
/// For many simple use cases, users will be better off using channels than a
/// Cond (Broadcast corresponds to closing a channel, and Signal corresponds to
/// sending on a channel).
/// 
/// For more on replacements for [sync.Cond], see [Roberto Clapis's series on
/// advanced concurrency patterns], as well as [Bryan Mills's talk on concurrency
/// patterns].
/// 
/// [the Go memory model]: https://go.dev/ref/mem
/// [Roberto Clapis's series on advanced concurrency patterns]: https://blogtitle.github.io/categories/concurrency/
/// [Bryan Mills's talk on concurrency patterns]: https://drive.google.com/file/d/1nPdvhB0PutEJzdCq5ms6UI58dp50fcAN/view
pub struct Cond {
  pub L: Option<Locker>,
}

/// A Locker represents an object that can be locked and unlocked.
pub interface Locker {
  fn Lock()
  fn Unlock()
}

/// Map is like a Go map[any]any but is safe for concurrent use
/// by multiple goroutines without additional locking or coordination.
/// Loads, stores, and deletes run in amortized constant time.
/// 
/// The Map type is specialized. Most code should use a plain Go map instead,
/// with separate locking or coordination, for better type safety and to make it
/// easier to maintain other invariants along with the map content.
/// 
/// The Map type is optimized for two common use cases: (1) when the entry for a given
/// key is only ever written once but read many times, as in caches that only grow,
/// or (2) when multiple goroutines read, write, and overwrite entries for disjoint
/// sets of keys. In these two cases, use of a Map may significantly reduce lock
/// contention compared to a Go map paired with a separate [Mutex] or [RWMutex].
/// 
/// The zero Map is empty and ready for use. A Map must not be copied after first use.
/// 
/// In the terminology of [the Go memory model], Map arranges that a write operation
/// “synchronizes before” any read operation that observes the effect of the write, where
/// read and write operations are defined as follows.
/// [Map.Load], [Map.LoadAndDelete], [Map.LoadOrStore], [Map.Swap], [Map.CompareAndSwap],
/// and [Map.CompareAndDelete] are read operations;
/// [Map.Delete], [Map.LoadAndDelete], [Map.Store], and [Map.Swap] are write operations;
/// [Map.LoadOrStore] is a write operation when it returns loaded set to false;
/// [Map.CompareAndSwap] is a write operation when it returns swapped set to true;
/// and [Map.CompareAndDelete] is a write operation when it returns deleted set to true.
/// 
/// [the Go memory model]: https://go.dev/ref/mem
pub type Map

/// A Mutex is a mutual exclusion lock.
/// The zero value for a Mutex is an unlocked mutex.
/// 
/// A Mutex must not be copied after first use.
/// 
/// In the terminology of [the Go memory model],
/// the n'th call to [Mutex.Unlock] “synchronizes before” the m'th call to [Mutex.Lock]
/// for any n < m.
/// A successful call to [Mutex.TryLock] is equivalent to a call to Lock.
/// A failed call to TryLock does not establish any “synchronizes before”
/// relation at all.
/// 
/// [the Go memory model]: https://go.dev/ref/mem
pub type Mutex

/// Once is an object that will perform exactly one action.
/// 
/// A Once must not be copied after first use.
/// 
/// In the terminology of [the Go memory model],
/// the return from f “synchronizes before”
/// the return from any call of once.Do(f).
/// 
/// [the Go memory model]: https://go.dev/ref/mem
pub type Once

/// A Pool is a set of temporary objects that may be individually saved and
/// retrieved.
/// 
/// Any item stored in the Pool may be removed automatically at any time without
/// notification. If the Pool holds the only reference when this happens, the
/// item might be deallocated.
/// 
/// A Pool is safe for use by multiple goroutines simultaneously.
/// 
/// Pool's purpose is to cache allocated but unused items for later reuse,
/// relieving pressure on the garbage collector. That is, it makes it easy to
/// build efficient, thread-safe free lists. However, it is not suitable for all
/// free lists.
/// 
/// An appropriate use of a Pool is to manage a group of temporary items
/// silently shared among and potentially reused by concurrent independent
/// clients of a package. Pool provides a way to amortize allocation overhead
/// across many clients.
/// 
/// An example of good use of a Pool is in the fmt package, which maintains a
/// dynamically-sized store of temporary output buffers. The store scales under
/// load (when many goroutines are actively printing) and shrinks when
/// quiescent.
/// 
/// On the other hand, a free list maintained as part of a short-lived object is
/// not a suitable use for a Pool, since the overhead does not amortize well in
/// that scenario. It is more efficient to have such objects implement their own
/// free list.
/// 
/// A Pool must not be copied after first use.
/// 
/// In the terminology of [the Go memory model], a call to Put(x) “synchronizes before”
/// a call to [Pool.Get] returning that same value x.
/// Similarly, a call to New returning x “synchronizes before”
/// a call to Get returning that same value x.
/// 
/// [the Go memory model]: https://go.dev/ref/mem
pub struct Pool {
  pub New: fn() -> Unknown,
}

/// A RWMutex is a reader/writer mutual exclusion lock.
/// The lock can be held by an arbitrary number of readers or a single writer.
/// The zero value for a RWMutex is an unlocked mutex.
/// 
/// A RWMutex must not be copied after first use.
/// 
/// If any goroutine calls [RWMutex.Lock] while the lock is already held by
/// one or more readers, concurrent calls to [RWMutex.RLock] will block until
/// the writer has acquired (and released) the lock, to ensure that
/// the lock eventually becomes available to the writer.
/// Note that this prohibits recursive read-locking.
/// A [RWMutex.RLock] cannot be upgraded into a [RWMutex.Lock],
/// nor can a [RWMutex.Lock] be downgraded into a [RWMutex.RLock].
/// 
/// In the terminology of [the Go memory model],
/// the n'th call to [RWMutex.Unlock] “synchronizes before” the m'th call to Lock
/// for any n < m, just as for [Mutex].
/// For any call to RLock, there exists an n such that
/// the n'th call to Unlock “synchronizes before” that call to RLock,
/// and the corresponding call to [RWMutex.RUnlock] “synchronizes before”
/// the n+1'th call to Lock.
/// 
/// [the Go memory model]: https://go.dev/ref/mem
pub type RWMutex

/// A WaitGroup is a counting semaphore typically used to wait
/// for a group of goroutines or tasks to finish.
/// 
/// Typically, a main goroutine will start tasks, each in a new
/// goroutine, by calling [WaitGroup.Go] and then wait for all tasks to
/// complete by calling [WaitGroup.Wait]. For example:
/// 
/// 	var wg sync.WaitGroup
/// 	wg.Go(task1)
/// 	wg.Go(task2)
/// 	wg.Wait()
/// 
/// A WaitGroup may also be used for tracking tasks without using Go to
/// start new goroutines by using [WaitGroup.Add] and [WaitGroup.Done].
/// 
/// The previous example can be rewritten using explicitly created
/// goroutines along with Add and Done:
/// 
/// 	var wg sync.WaitGroup
/// 	wg.Add(1)
/// 	go func() {
/// 		defer wg.Done()
/// 		task1()
/// 	}()
/// 	wg.Add(1)
/// 	go func() {
/// 		defer wg.Done()
/// 		task2()
/// 	}()
/// 	wg.Wait()
/// 
/// This pattern is common in code that predates [WaitGroup.Go].
/// 
/// A WaitGroup must not be copied after first use.
pub type WaitGroup

impl Cond {
  /// Broadcast wakes all goroutines waiting on c.
  /// 
  /// It is allowed but not required for the caller to hold c.L
  /// during the call.
  fn Broadcast(self: Ref<Cond>)

  /// Signal wakes one goroutine waiting on c, if there is any.
  /// 
  /// It is allowed but not required for the caller to hold c.L
  /// during the call.
  /// 
  /// Signal() does not affect goroutine scheduling priority; if other goroutines
  /// are attempting to lock c.L, they may be awoken before a "waiting" goroutine.
  fn Signal(self: Ref<Cond>)

  /// Wait atomically unlocks c.L and suspends execution
  /// of the calling goroutine. After later resuming execution,
  /// Wait locks c.L before returning. Unlike in other systems,
  /// Wait cannot return unless awoken by [Cond.Broadcast] or [Cond.Signal].
  /// 
  /// Because c.L is not locked while Wait is waiting, the caller
  /// typically cannot assume that the condition is true when
  /// Wait returns. Instead, the caller should Wait in a loop:
  /// 
  /// 	c.L.Lock()
  /// 	for !condition() {
  /// 	    c.Wait()
  /// 	}
  /// 	... make use of condition ...
  /// 	c.L.Unlock()
  fn Wait(self: Ref<Cond>)
}

impl Map {
  /// Clear deletes all the entries, resulting in an empty Map.
  fn Clear(self: Ref<Map>)

  /// CompareAndDelete deletes the entry for key if its value is equal to old.
  /// The old value must be of a comparable type.
  /// 
  /// If there is no current value for key in the map, CompareAndDelete
  /// returns false (even if the old value is the nil interface value).
  fn CompareAndDelete(self: Ref<Map>, key: Unknown, old: Unknown) -> bool

  /// CompareAndSwap swaps the old and new values for key
  /// if the value stored in the map is equal to old.
  /// The old value must be of a comparable type.
  fn CompareAndSwap(self: Ref<Map>, key: Unknown, old: Unknown, new: Unknown) -> bool

  /// Delete deletes the value for a key.
  /// If the key is not in the map, Delete does nothing.
  fn Delete(self: Ref<Map>, key: Unknown)

  /// Load returns the value stored in the map for a key, or nil if no
  /// value is present.
  /// The ok result indicates whether value was found in the map.
  fn Load(self: Ref<Map>, key: Unknown) -> Option<Unknown>

  /// LoadAndDelete deletes the value for a key, returning the previous value if any.
  /// The loaded result reports whether the key was present.
  fn LoadAndDelete(self: Ref<Map>, key: Unknown) -> (Unknown, bool)

  /// LoadOrStore returns the existing value for the key if present.
  /// Otherwise, it stores and returns the given value.
  /// The loaded result is true if the value was loaded, false if stored.
  fn LoadOrStore(self: Ref<Map>, key: Unknown, value: Unknown) -> (Unknown, bool)

  /// Range calls f sequentially for each key and value present in the map.
  /// If f returns false, range stops the iteration.
  /// 
  /// Range does not necessarily correspond to any consistent snapshot of the Map's
  /// contents: no key will be visited more than once, but if the value for any key
  /// is stored or deleted concurrently (including by f), Range may reflect any
  /// mapping for that key from any point during the Range call. Range does not
  /// block other methods on the receiver; even f itself may call any method on m.
  /// 
  /// Range may be O(N) with the number of elements in the map even if f returns
  /// false after a constant number of calls.
  fn Range(self: Ref<Map>, f: fn(Unknown, Unknown) -> bool)

  /// Store sets the value for a key.
  fn Store(self: Ref<Map>, key: Unknown, value: Unknown)

  /// Swap swaps the value for a key and returns the previous value if any.
  /// The loaded result reports whether the key was present.
  fn Swap(self: Ref<Map>, key: Unknown, value: Unknown) -> (Unknown, bool)
}

impl Mutex {
  /// Lock locks m.
  /// If the lock is already in use, the calling goroutine
  /// blocks until the mutex is available.
  fn Lock(self: Ref<Mutex>)

  /// TryLock tries to lock m and reports whether it succeeded.
  /// 
  /// Note that while correct uses of TryLock do exist, they are rare,
  /// and use of TryLock is often a sign of a deeper problem
  /// in a particular use of mutexes.
  fn TryLock(self: Ref<Mutex>) -> bool

  /// Unlock unlocks m.
  /// It is a run-time error if m is not locked on entry to Unlock.
  /// 
  /// A locked [Mutex] is not associated with a particular goroutine.
  /// It is allowed for one goroutine to lock a Mutex and then
  /// arrange for another goroutine to unlock it.
  fn Unlock(self: Ref<Mutex>)
}

impl Once {
  /// Do calls the function f if and only if Do is being called for the
  /// first time for this instance of [Once]. In other words, given
  /// 
  /// 	var once Once
  /// 
  /// if once.Do(f) is called multiple times, only the first call will invoke f,
  /// even if f has a different value in each invocation. A new instance of
  /// Once is required for each function to execute.
  /// 
  /// Do is intended for initialization that must be run exactly once. Since f
  /// is niladic, it may be necessary to use a function literal to capture the
  /// arguments to a function to be invoked by Do:
  /// 
  /// 	config.once.Do(func() { config.init(filename) })
  /// 
  /// Because no call to Do returns until the one call to f returns, if f causes
  /// Do to be called, it will deadlock.
  /// 
  /// If f panics, Do considers it to have returned; future calls of Do return
  /// without calling f.
  fn Do(self: Ref<Once>, f: fn() -> ())
}

impl Pool {
  /// Get selects an arbitrary item from the [Pool], removes it from the
  /// Pool, and returns it to the caller.
  /// Get may choose to ignore the pool and treat it as empty.
  /// Callers should not assume any relation between values passed to [Pool.Put] and
  /// the values returned by Get.
  /// 
  /// If Get would otherwise return nil and p.New is non-nil, Get returns
  /// the result of calling p.New.
  fn Get(self: Ref<Pool>) -> Unknown

  /// Put adds x to the pool.
  fn Put(self: Ref<Pool>, x: Unknown)
}

impl RWMutex {
  /// Lock locks rw for writing.
  /// If the lock is already locked for reading or writing,
  /// Lock blocks until the lock is available.
  fn Lock(self: Ref<RWMutex>)

  /// RLock locks rw for reading.
  /// 
  /// It should not be used for recursive read locking; a blocked Lock
  /// call excludes new readers from acquiring the lock. See the
  /// documentation on the [RWMutex] type.
  fn RLock(self: Ref<RWMutex>)

  /// RLocker returns a [Locker] interface that implements
  /// the [Locker.Lock] and [Locker.Unlock] methods by calling rw.RLock and rw.RUnlock.
  fn RLocker(self: Ref<RWMutex>) -> Locker

  /// RUnlock undoes a single [RWMutex.RLock] call;
  /// it does not affect other simultaneous readers.
  /// It is a run-time error if rw is not locked for reading
  /// on entry to RUnlock.
  fn RUnlock(self: Ref<RWMutex>)

  /// TryLock tries to lock rw for writing and reports whether it succeeded.
  /// 
  /// Note that while correct uses of TryLock do exist, they are rare,
  /// and use of TryLock is often a sign of a deeper problem
  /// in a particular use of mutexes.
  fn TryLock(self: Ref<RWMutex>) -> bool

  /// TryRLock tries to lock rw for reading and reports whether it succeeded.
  /// 
  /// Note that while correct uses of TryRLock do exist, they are rare,
  /// and use of TryRLock is often a sign of a deeper problem
  /// in a particular use of mutexes.
  fn TryRLock(self: Ref<RWMutex>) -> bool

  /// Unlock unlocks rw for writing. It is a run-time error if rw is
  /// not locked for writing on entry to Unlock.
  /// 
  /// As with Mutexes, a locked [RWMutex] is not associated with a particular
  /// goroutine. One goroutine may [RWMutex.RLock] ([RWMutex.Lock]) a RWMutex and then
  /// arrange for another goroutine to [RWMutex.RUnlock] ([RWMutex.Unlock]) it.
  fn Unlock(self: Ref<RWMutex>)
}

impl WaitGroup {
  /// Add adds delta, which may be negative, to the [WaitGroup] task counter.
  /// If the counter becomes zero, all goroutines blocked on [WaitGroup.Wait] are released.
  /// If the counter goes negative, Add panics.
  /// 
  /// Callers should prefer [WaitGroup.Go].
  /// 
  /// Note that calls with a positive delta that occur when the counter is zero
  /// must happen before a Wait. Calls with a negative delta, or calls with a
  /// positive delta that start when the counter is greater than zero, may happen
  /// at any time.
  /// Typically this means the calls to Add should execute before the statement
  /// creating the goroutine or other event to be waited for.
  /// If a WaitGroup is reused to wait for several independent sets of events,
  /// new Add calls must happen after all previous Wait calls have returned.
  /// See the WaitGroup example.
  fn Add(self: Ref<WaitGroup>, delta: int)

  /// Done decrements the [WaitGroup] task counter by one.
  /// It is equivalent to Add(-1).
  /// 
  /// Callers should prefer [WaitGroup.Go].
  /// 
  /// In the terminology of [the Go memory model], a call to Done
  /// "synchronizes before" the return of any Wait call that it unblocks.
  /// 
  /// [the Go memory model]: https://go.dev/ref/mem
  fn Done(self: Ref<WaitGroup>)

  /// Go calls f in a new goroutine and adds that task to the [WaitGroup].
  /// When f returns, the task is removed from the WaitGroup.
  /// 
  /// The function f must not panic.
  /// 
  /// If the WaitGroup is empty, Go must happen before a [WaitGroup.Wait].
  /// Typically, this simply means Go is called to start tasks before Wait is called.
  /// If the WaitGroup is not empty, Go may happen at any time.
  /// This means a goroutine started by Go may itself call Go.
  /// If a WaitGroup is reused to wait for several independent sets of tasks,
  /// new Go calls must happen after all previous Wait calls have returned.
  /// 
  /// In the terminology of [the Go memory model], the return from f
  /// "synchronizes before" the return of any Wait call that it unblocks.
  /// 
  /// [the Go memory model]: https://go.dev/ref/mem
  fn Go(self: Ref<WaitGroup>, f: fn() -> ())

  /// Wait blocks until the [WaitGroup] task counter is zero.
  fn Wait(self: Ref<WaitGroup>)
}