@hazae41/box
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -9,3 +9,3 @@ import { Copiable, Copied } from '../copy/copy.js'; | ||
} | ||
declare class Box<T extends Disposable> { | ||
declare class Box<T extends Disposable> implements Disposable { | ||
readonly inner: T; | ||
@@ -18,2 +18,3 @@ moved: boolean; | ||
[Symbol.dispose](): void; | ||
static wrap<T extends Disposable>(inner: T): Box<T>; | ||
/** | ||
@@ -20,0 +21,0 @@ * Just get the inner value |
@@ -8,4 +8,5 @@ /** | ||
[Symbol.dispose](): void; | ||
static wrap<T extends Disposable>(inner: T): Slot<T>; | ||
} | ||
export { Slot }; |
{ | ||
"type": "module", | ||
"name": "@hazae41/box", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Rust-like Box for TypeScript", | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/hazae41/box", |
@@ -45,1 +45,53 @@ # Box | ||
``` | ||
## Rules | ||
1. You can't pass a disposable object without wrapping it in a Box | ||
2. You can't hold a disposable object without wrapping it in a Box | ||
3. You can't hold a Box without owning it and disposing it after | ||
4. You can't return a Box without unwrapping it | ||
This means the typical object holding a Box looks like this | ||
```tsx | ||
import { Box } from "@hazae41/box" | ||
class MyWrapper<T extends Disposable> { | ||
private constructor( | ||
/** | ||
* Rule 2. hold as box | ||
**/ | ||
readonly box: Box<T> | ||
) {} | ||
[Symbol.dispose]() { | ||
/** | ||
* Rule 3. dispose any box you hold | ||
**/ | ||
this.box[Symbol.dispose]() | ||
} | ||
static create<T extends Disposable>(box: Box<T>) { | ||
/** | ||
* Rule 3. own any box you want to hold | ||
**/ | ||
return new MyWrapper(box.move()) | ||
} | ||
use() { | ||
/** | ||
* Rule 1. only pass as box | ||
**/ | ||
something(this.box) | ||
} | ||
export(): T { | ||
/** | ||
* Rule 4. unwrap on return | ||
**/ | ||
return this.box.unwrap() | ||
} | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28606
446
97