chanutil

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2023 License: MIT Imports: 3 Imported by: 2

README

chanutil

Go Reference Tests

Some utilities for interacting with channels that I use in my code.

  • Get - receive from a channel in a blocking manner, but return if a context expires.
  • Put - the same, for sending to a channel.

Currently, it's so small, that it probably makes no sense to use it. Feel free to just steal the code you want, no need to introduce unnecessary dependency on this lib. It's MIT.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrResultSentTwice = fmt.Errorf("tried to send result twice")

Functions

func Get

func Get[T any](ctx context.Context, ch <-chan T) (*T, bool, error)

Get tries to read a message from a channel, blocking until it succeeds or the context gets cancelled or expires.

Returns a pointer to the message if it was received successfuly (nil otherwise), a bool equal to true if the channel isn't closed, and ctx.Err() if the context expired/got cancelled.

func Put

func Put[T any](ctx context.Context, ch chan<- T, v T) error

Put tries to put a message in a channel, blocking until it succeeds or the context gets cancelled or expires. Returns ctx.Err() if the message was not sent successfuly.

Types

type Request added in v0.2.0

type Request[Args any, Ret any] struct {
	// contains filtered or unexported fields
}

func (*Request[Args, Ret]) Args added in v0.2.0

func (r *Request[Args, Ret]) Args() Args

func (*Request[Args, Ret]) Context added in v0.2.0

func (r *Request[Args, Ret]) Context() context.Context

func (*Request[Args, Ret]) Err added in v0.2.0

func (r *Request[Args, Ret]) Err(err error) error

func (*Request[Args, Ret]) Ok added in v0.2.0

func (r *Request[Args, Ret]) Ok(ret Ret) error

type RequestChannel added in v0.2.0

type RequestChannel[Args any, Ret any] chan *Request[Args, Ret]

func (RequestChannel[Args, Ret]) Call added in v0.2.0

func (ch RequestChannel[Args, Ret]) Call(ctx context.Context, args Args) (*Ret, error)

type Result added in v0.2.0

type Result[T any] struct {
	// contains filtered or unexported fields
}

func (Result[T]) Unpack added in v0.2.0

func (r Result[T]) Unpack() (*T, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL