risky

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package risky contains a bunch of bad ideas for APIs and operations that will definitely lead to panics and deadlocks and incorrect behavior when used incorrectly.

At the same time, these operations may be useful in the right situations, and the package name "risky" indicates (like "unsafe") that the operation should be handled with care.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply added in v0.10.0

func Apply[T any, O any](fn func(T) (O, error), arg T) O

Apply runs a function that takes an arbitrary argument and ignores the error and swallows any panic, returning the output of the function, likely a Zero value, in the case of an error. This is a risky move: usually functions panic for a reason, but for certain invariants this may be useful.

Be aware, that while Ignore will recover from any panics, defers within the ignored function will not run unless there is a call to recover *before* the defer.

func ApplyAll added in v0.13.0

func ApplyAll[T any, O any](fn func(T) O, in []T) (out []O)

ApplyAll processes an input slice, with the provided function, returning a new slice that holds the results. Panics are ignored and do not abort the operation. The output is always the same length as the input.

func ApplyDefault added in v0.13.0

func ApplyDefault[T any](op func(T) (T, error), input T) (out T)

ApplyDefault runs a function with the provided input, and returns the input. If the operation panics or returns an error, ApplyDefault returns the input argument.

func Block

func Block[T any](fn func(context.Context) (T, error)) (T, error)

Block runs a function with a background Context, like Block.

func BlockForce added in v0.13.0

func BlockForce[T any](fn func(context.Context) T) T

BlockForce runs the function with a context that is never canceled. Use this in cases where you don't want to plumb a context through *and* the operation cannot block on the context.

func BlockForceIgnore added in v0.13.0

func BlockForceIgnore[T any](fn func(context.Context) (T, error)) T

BlockForceIgnore combines Block with ForceOp to run a function with a context that is never canceled, ignoring any panics and returning the output value.

func Cast added in v0.10.0

func Cast[T any](in any) T

Cast just provides as a wrapper around in.(T). Cast will panic.

func Check

func Check[T any](out T, err error) (T, bool)

Check takes two values and returns the first value and a second "ok" value. The second value is true if the error is nil (isOK) and false otherwise. This is not too risky.

func Force

func Force[T any](out T, _ error) T

Force swallows an error, and returns the output, as a non-panic'ing form of risky.Force.

size += risky.Force(buffer.Write([]byte("hello world")))

func ForceCall added in v0.13.0

func ForceCall[T any](fn func() (T, error)) T

ForceCall takes the function and calls it itself so that it can ignore a possible panic and return an output. In the case of a panic or an error the output value is often the zero value for the type.

func Ignore

func Ignore(error)

Ignore swallows an error and is a noop, but provides a level of annotation beyond assigning to the empty identifier.

_ = operation()

vs:

ft.Ignore(operation())

func Recover

func Recover()

Recover catches a panic and discards its value.

func Slice added in v0.10.0

func Slice[T any](iter *fun.Stream[T]) []T

Slice converts a stream into a slice: this will not abort or timeout if the stream is blocking. The streams close method is not processed.

func WithRecover added in v0.13.0

func WithRecover[T any](fn func(T) error, arg T)

WithRecover runs a function that takes an arbitrary argument and ignores the error and swallows any panic. This is a risky move: usually functions panic for a reason, but for certain invariants this may be useful.

Be aware, that while WithRecover will recover from any panics, defers within the ignored function will not run unless there is a call to recover *before* the defer.

Types

This section is empty.

Jump to

Keyboard shortcuts

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