UNPKG

option-t

Version:

A toolkit of Nullable/Option/Result type implementation in ECMAScript. Their APIs are inspired by Rust's `Option<T>` and `Result<T, E>`.

78 lines (77 loc) 3.2 kB
import type { EffectFn } from '../../internal/function.js'; import type { MutOk, MutErr } from '../internal/mutable.js'; import { type Result } from '../result.js'; export type { MutOk, MutErr }; export type UnsafeOkDestructorFn<in T> = EffectFn<MutOk<T>>; export type UnsafeErrDestructorFn<in E> = EffectFn<MutErr<E>>; /** * mutators ared called for the inputted _input_. * * This function allows you to cut the reference to contain values from _input_. * However, it would be unsafe operation if you do some more operation after this. * For example, you can write the following code. * * ```javascript * const ok = createOk(1); * unsafeDropForResult(ok, (ok) => { * ok.ok = false; * }, (err) => { * ok.ok = true; * }); * isOk(ok); // -> false. This is dangerous. * ``` * * Compared to Rust, JavaScript does not have ownership semantics in language * and this API is designed to use as a destructor or similar fashions. * So if you call this for same object more than once, your code might contain "double free" problem. * * @throws * This throw an `Error` instance if the _input_ is frozen. */ export declare function unsafeDropBothForResult<T, E>(input: Result<T, E>, okMutator: UnsafeOkDestructorFn<T>, errMutator: UnsafeErrDestructorFn<E>): void; /** * The _okMutator_ is called if _input_ is `Ok<T>`. * * This function allows you to cut the reference to the contain value from _input_. * However, it would be unsafe operation if you do some more operation after this. * For example, you can write the following code. * * ```javascript * const ok = createOk(1); * unsafeDropOkForResult(ok, (ok) => { * ok.ok = false; * }); * isOk(ok); // -> false. This is dangerous. * ``` * * Compared to Rust, JavaScript does not have ownership semantics in language * and this API is designed to use as a destructor or similar fashions. * So if you call this for same object more than once, your code might contain "double free" problem. * * @throws * This throw an `Error` instance if the _input_ is frozen. */ export declare function unsafeDropOkForResult<T, E>(input: Result<T, E>, okMutator: UnsafeOkDestructorFn<T>): void; /** * The _errMutator_ is called if _input_ is `Err<E>`. * * This function allows you to cut the reference to the contain value from _input_. * However, it would be unsafe operation if you do some more operation after this. * For example, you can write the following code. * * ```javascript * const err = createErr('bar'); * unsafeDropErrForResult(err, (err) => { * err.ok = true; * }); * isErr(err); // -> false. This is dangerous. * ``` * * Compared to Rust, JavaScript does not have ownership semantics in language * and this API is designed to use as a destructor or similar fashions. * So if you call this for same object more than once, your code might contain "double free" problem. * * @throws * This throw an `Error` instance if the _input_ is frozen. */ export declare function unsafeDropErrForResult<T, E>(input: Result<T, E>, errMutator: UnsafeErrDestructorFn<E>): void;