{"modulePath":"github.com/dispatchrun/coroutine","version":"v0.9.3","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package coroutine\n\n## Constants\n\n```go\nconst Durable = false\n```\nDurable is a constant which takes the values true or false depending on whether the program is built with the \"durable\" tag.\n\n## Variables\n\n```go\nvar (\n\t// ErrNotDurable is an error that occurs when attempting to\n\t// serialize a coroutine that is not durable.\n\tErrNotDurable = errors.New(\"only durable coroutines can be serialized\")\n\n\t// ErrInvalidState is an error that occurs when attempting to\n\t// deserialize a coroutine that was serialized in another build.\n\tErrInvalidState = errors.New(\"durable coroutine was serialized in another build\")\n)\n```\n\n## Functions\n\n```go\nfunc Run[R, S any](c Coroutine[R, S], f func(R) S) R\n```\nRun executes a coroutine to completion, calling f for each value that the coroutine yields, and sending back each value that f returns.\n\nIf c was constructed with NewWithReturn, the return value of the coroutine is returned by Run. Otherwise, the zero-value is returned and can be ignored by the caller.\n\n```go\nfunc Yield[R, S any](v R) S\n```\nYield sends v to the generator and pauses the execution of the coroutine until the Next method is called on the associated generator.\n\nThe function panics when called on a stack where no active coroutine exists, or if the type parameters do not match those of the coroutine.\n\n## Types\n\n```go\ntype Context[R, S any] struct {\n\t// contains filtered or unexported fields\n}\n```\nContext is passed to a coroutine and flows through all functions that Yield (or could yield).\n\n```go\nfunc LoadContext[R, S any]() *Context[R, S]\n```\nLoadContext returns the context for the current coroutine.\n\nThe function panics when called on a stack where no active coroutine exists, or if the type parameters do not match those of the coroutine.\n\n```go\nfunc (c *Context[R, S]) Marshal() ([]byte, error)\n```\n\n```go\nfunc (c *Context[R, S]) Unmarshal(b []byte) error\n```\n\n```go\nfunc (c *Context[R, S]) Yield(v R) S\n```\n\n```go\ntype Coroutine[R, S any] struct {\n\t// contains filtered or unexported fields\n}\n```\nCoroutine instances expose APIs allowing the program to drive the execution of coroutines.\n\nThe type parameter R represents the type of values that the program can receive from the coroutine (what it yields), and the type parameter S is what the program can send back to a coroutine yield point.\n\n```go\nfunc New[R, S any](f func()) Coroutine[R, S]\n```\nNew creates a new coroutine which executes f as entry point.\n\n```go\nfunc NewWithReturn[R, S any](f func() R) Coroutine[R, S]\n```\nNew creates a new coroutine which executes f as entry point.\n\n```go\nfunc (c Coroutine[R, S]) Context() *Context[R, S]\n```\nContext returns the coroutine's associated Context.\n\n```go\nfunc (c Coroutine[R, S]) Done() bool\n```\nDone returns true if the coroutine completed, either because it was stopped or because its function returned.\n\n```go\nfunc (c Coroutine[R, S]) Next() bool\n```\nNext executes the coroutine until its next yield point, or until completion. The method returns true if the coroutine entered a yield point, after which the program should call Recv to obtain the value that the coroutine yielded, and Send to set the value that will be returned from the yield point.\n\n```go\nfunc (c Coroutine[R, S]) Recv() R\n```\nRecv returns the last value that the coroutine has yielded. The method must be called only after a call to Next has returned true, or the return value is undefined. Calling the method multiple times after a call to Next returns the same value each time.\n\n```go\nfunc (c Coroutine[R, S]) Result() R\n```\nResult is the return value of the coroutine, if it was constructed with NewWithReturn. Result should only be called once Next returns false, indicating that the coroutine finished executing.\n\n```go\nfunc (c Coroutine[R, S]) Send(v S)\n```\nSend sets the value that will be seen by the coroutine after it resumes from a yield point. Calling the method multiple times before a call to Next does not result in sending multiple values, only the last value sent will be seen by the coroutine.\n\n```go\nfunc (c Coroutine[R, S]) Stop()\n```\nStop interrupts the coroutine. On the next call to Next, the coroutine will not return from its yield point; instead, it unwinds its call stack, calling each defer statement in the inverse order that they were declared.\n\nStop is idempotent, calling it multiple times or after completion of the coroutine has no effect.\n\nThis method is just an interrupt mechanism, the program does not have to call it to release the coroutine resources after completion.\n\n","path":"github.com/dispatchrun/coroutine","name":"coroutine","synopsis":"","isRedistributable":true}
