Documentation
¶
Overview ¶
Package coro provides an implementation of coroutines built on top of Go's goroutines for the execution, suspension and resuming of generalized subroutines and functions for cooperative multitasking.
Based on the wonderful blog post shared by Rus Cox.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrStopped = errors.New("stop generator")
ErrStopped signals to the generator that it must stop.
Functions ¶
func IsStopped ¶ added in v1.1.0
IsStopped tests whenever the generator was stopped.
Used to check if a recovered panic value, that was retrieved via the builtin recover function, was an ErrStopped derived value sent by the C.Stop function.
func PullFn ¶
func PullFn[I, O any](fn GeneratorFn[I, O]) (func(I) (O, bool), func())
func PullSub ¶
func PullSub[I, O any](fn GeneratorSub[I, O]) (func(I) (O, bool), func())
Types ¶
type C ¶
type C[I, O any] struct { // contains filtered or unexported fields }
C represents a coroutine encapsulating a generator that is executed in a goroutine.
func NewFn ¶
func NewFn[I, O any](fn GeneratorFn[I, O]) *C[I, O]
NewFn encapsulates the given GeneratorFn generator in a C coroutine.
func NewSub ¶
func NewSub[I, O any](fn GeneratorSub[I, O]) *C[I, O]
NewSub encapsulates the given GeneratorSub generator in a C coroutine.
func (*C[I, O]) Resume ¶
Resume resumes the coroutine.
Resume sends a given value to the encapsulated generator, causing the encapsulated generator to resume its operation and blocks the caller until the generator sends back a new value, returns or panics.
func (*C[I, O]) Stop ¶
func (cr *C[I, O]) Stop()
Stop stops the coroutine.
Stop causes the encapsulated generator to panic with an ErrStopped derived value on the next invocation of YieldFn.
It is expected that the generator does not recover from the panic invocation and returns from its execution.
type GeneratorFn ¶
type GeneratorFn[I, O any] func(I, func(O) I) O
GeneratorFn is a function that generates values which are sent back through the given YieldFn function.
The function has a return value that will be sent back as the last value generated by the generator.
type GeneratorSub ¶
type GeneratorSub[I, O any] func(I, func(O) I)
GeneratorSub is a subroutine (a function without a return value) that generates values which are sent back through the given YieldFn function.
Contrary to GeneratorFn the function has no return value that will be sent back as the last value produced by the generator.