UNPKG

@hazae41/box

Version:

Rust-like Box and similar objects for TypeScript

70 lines (67 loc) 1.95 kB
import { Nullable } from '../../libs/nullable/index.js'; import { Ref } from '../ref/index.js'; import { Wrap } from '../wrap/index.js'; /** * A movable and borrowable reference */ declare class Box<T> implements Disposable { #private; readonly value: T; readonly clean: Disposable; /** * An movable and borrowable reference * @param value */ constructor(value: T, clean: Disposable); static wrap<T extends Disposable>(value: T): Box<T>; static from<T>(value: Wrap<T>): Box<T>; static with<T>(value: T, clean: (value: T) => void): Box<T>; [Symbol.dispose](): void; [Symbol.asyncDispose](): Promise<void>; get owned(): boolean; get borrowed(): boolean; get moved(): boolean; /** * Get the value * @returns T */ get(): T; /** * Get the value or null-like if not owned * @returns T or null-like if not owned */ getOrNull(): Nullable<T>; /** * Get the value or throw if not owned * @returns T * @throws NotOwnedError if not owned */ getOrThrow(): T; checkOrNull(): Nullable<this>; checkOrThrow(): this; /** * Get the value and set this as moved or null-like if not owned * @returns T or null-like if not owned */ unwrapOrNull(): Nullable<T>; /** * Get the value and set this as moved or throw if not owned * @returns T * @throws BoxMovedError if not owned */ unwrapOrThrow(): T; /** * Move the value to a new box and set this one as moved or null-like if already moved * @returns Box<T> or null-like if moved */ moveOrNull(): Nullable<Box<T>>; /** * Move the value to a new box and set this one as moved or throw if already moved * @returns Box<T> * @throws BoxMovedError if already moved */ moveOrThrow(): Box<T>; borrowOrNull(): Nullable<Ref<T>>; borrowOrThrow(): Ref<T>; } export { Box };