forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defer.go
43 lines (39 loc) · 981 Bytes
/
defer.go
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
package circuitdefer
import (
"github.com/airchains-network/gnark/internal/kvstore"
)
type deferKey struct{}
func Put[T any](builder any, cb T) {
// we use generics for type safety but to avoid import cycles.
// TODO: compare with using any and type asserting at caller
kv, ok := builder.(kvstore.Store)
if !ok {
panic("builder does not implement kvstore.Store")
}
val := kv.GetKeyValue(deferKey{})
var deferred []T
if val != nil {
var ok bool
deferred, ok = val.([]T)
if !ok {
panic("stored deferred functions not []func(frontend.API) error")
}
}
deferred = append(deferred, cb)
kv.SetKeyValue(deferKey{}, deferred)
}
func GetAll[T any](builder any) []T {
kv, ok := builder.(kvstore.Store)
if !ok {
panic("builder does not implement kvstore.Store")
}
val := kv.GetKeyValue(deferKey{})
if val == nil {
return nil
}
deferred, ok := val.([]T)
if !ok {
panic("stored deferred functions not []func(frontend.API) error")
}
return deferred
}