happy-rusty 1.9.0 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/dist/main.cjs +2255 -1453
- package/dist/main.cjs.map +1 -1
- package/dist/main.mjs +2254 -1449
- package/dist/main.mjs.map +1 -1
- package/dist/types.d.ts +54 -103
- package/package.json +13 -13
package/dist/types.d.ts
CHANGED
|
@@ -41,6 +41,7 @@ pub enum Result<T, E> {
|
|
|
41
41
|
```
|
|
42
42
|
* @typeParam T - The type of the value contained in a successful `Result`.
|
|
43
43
|
* @typeParam E - The type of the error contained in an unsuccessful `Result`.
|
|
44
|
+
* @since 1.0.0
|
|
44
45
|
* @see https://doc.rust-lang.org/std/result/enum.Result.html
|
|
45
46
|
*/
|
|
46
47
|
interface Result<T, E> {
|
|
@@ -663,6 +664,8 @@ interface Result<T, E> {
|
|
|
663
664
|
*/
|
|
664
665
|
asErr<U>(): Result<U, E>;
|
|
665
666
|
/**
|
|
667
|
+
* **Non-standard extension**: This method is not part of Rust's standard library.
|
|
668
|
+
*
|
|
666
669
|
* Like `andThenAsync`, but automatically catches any thrown exceptions or Promise rejections
|
|
667
670
|
* and converts them to `Err`.
|
|
668
671
|
*
|
|
@@ -676,6 +679,7 @@ interface Result<T, E> {
|
|
|
676
679
|
* @typeParam U - The type of the value returned by the function.
|
|
677
680
|
* @param fn - A function that takes the `Ok` value and returns `PromiseLike<U>` or `U`. May throw or reject.
|
|
678
681
|
* @returns A promise that resolves to `Ok(U)` if successful, or `Err(E)` if `this` is `Err` or if `fn` throws/rejects.
|
|
682
|
+
* @since 1.9.0
|
|
679
683
|
* @see andThenAsync
|
|
680
684
|
* @see tryAsyncResult
|
|
681
685
|
* @example
|
|
@@ -703,6 +707,8 @@ interface Result<T, E> {
|
|
|
703
707
|
*/
|
|
704
708
|
andTryAsync<U>(fn: (value: T) => PromiseLike<U> | U): AsyncResult<Awaited<U>, E>;
|
|
705
709
|
/**
|
|
710
|
+
* **Non-standard extension**: This method is not part of Rust's standard library.
|
|
711
|
+
*
|
|
706
712
|
* Like `orElseAsync`, but automatically catches any thrown exceptions or Promise rejections
|
|
707
713
|
* and converts them to `Err`.
|
|
708
714
|
*
|
|
@@ -716,6 +722,7 @@ interface Result<T, E> {
|
|
|
716
722
|
* @typeParam F - The type of the error returned by the function.
|
|
717
723
|
* @param fn - A function that takes the `Err` value and returns `PromiseLike<T>` or `T`. May throw or reject.
|
|
718
724
|
* @returns A promise that resolves to `Ok(T)` if `this` is `Ok` or if `fn` succeeds, or `Err(F)` if `fn` throws/rejects.
|
|
725
|
+
* @since 1.9.0
|
|
719
726
|
* @see orElseAsync
|
|
720
727
|
* @see tryAsyncResult
|
|
721
728
|
* @example
|
|
@@ -749,6 +756,7 @@ interface Result<T, E> {
|
|
|
749
756
|
*
|
|
750
757
|
* @typeParam T - The type of the value that is produced by a successful operation.
|
|
751
758
|
* @typeParam E - The type of the error that may be produced by a failed operation.
|
|
759
|
+
* @since 1.5.0
|
|
752
760
|
* @example
|
|
753
761
|
* ```ts
|
|
754
762
|
* async function fetchUser(id: number): AsyncResult<User, Error> {
|
|
@@ -772,6 +780,7 @@ type AsyncResult<T, E> = Promise<Result<T, E>>;
|
|
|
772
780
|
*
|
|
773
781
|
* @typeParam T - The type of the value that is produced by a successful operation.
|
|
774
782
|
* @typeParam E - The type of the error that may be produced by a failed operation.
|
|
783
|
+
* @since 1.8.0
|
|
775
784
|
* @example
|
|
776
785
|
* ```ts
|
|
777
786
|
* // Works with any PromiseLike, not just Promise
|
|
@@ -823,6 +832,7 @@ pub enum Option<T> {
|
|
|
823
832
|
}
|
|
824
833
|
```
|
|
825
834
|
* @typeParam T - The type of the value contained in the `Some` variant.
|
|
835
|
+
* @since 1.0.0
|
|
826
836
|
* @see https://doc.rust-lang.org/std/option/enum.Option.html
|
|
827
837
|
*/
|
|
828
838
|
interface Option<T> {
|
|
@@ -1429,6 +1439,7 @@ interface Option<T> {
|
|
|
1429
1439
|
* and return an `Option` as the result.
|
|
1430
1440
|
*
|
|
1431
1441
|
* @typeParam T - The type of the value that may be contained in the `Some` variant.
|
|
1442
|
+
* @since 1.5.0
|
|
1432
1443
|
* @example
|
|
1433
1444
|
* ```ts
|
|
1434
1445
|
* async function fetchUser(id: number): AsyncOption<User> {
|
|
@@ -1447,6 +1458,7 @@ type AsyncOption<T> = Promise<Option<T>>;
|
|
|
1447
1458
|
* allowing compatibility with any thenable object.
|
|
1448
1459
|
*
|
|
1449
1460
|
* @typeParam T - The type of the value that may be contained in the `Some` variant.
|
|
1461
|
+
* @since 1.8.0
|
|
1450
1462
|
* @example
|
|
1451
1463
|
* ```ts
|
|
1452
1464
|
* // Works with any PromiseLike, not just Promise
|
|
@@ -1470,7 +1482,7 @@ type AsyncLikeOption<T> = PromiseLike<Option<T>>;
|
|
|
1470
1482
|
* @param fn - A function that may throw an exception.
|
|
1471
1483
|
* @param args - Arguments to pass to the function.
|
|
1472
1484
|
* @returns `Some<T>` if the function succeeds, or `None` if it throws.
|
|
1473
|
-
*
|
|
1485
|
+
* @since 1.7.0
|
|
1474
1486
|
* @example
|
|
1475
1487
|
* ```ts
|
|
1476
1488
|
* // Parse JSON, ignore error details
|
|
@@ -1501,7 +1513,7 @@ declare function tryOption<T, Args extends unknown[]>(fn: (...args: Args) => T,
|
|
|
1501
1513
|
* @typeParam T - The type of the value that the promise resolves to.
|
|
1502
1514
|
* @param task - A promise or promise-like object to await.
|
|
1503
1515
|
* @returns A promise that resolves to `Some<T>` if successful, or `None` if rejected.
|
|
1504
|
-
*
|
|
1516
|
+
* @since 1.7.0
|
|
1505
1517
|
* @example
|
|
1506
1518
|
* ```ts
|
|
1507
1519
|
* // Fetch data, ignore error details
|
|
@@ -1529,7 +1541,7 @@ declare function tryAsyncOption<T>(task: PromiseLike<T>): AsyncOption<Awaited<T>
|
|
|
1529
1541
|
* @param task - A function that returns a value or promise-like object.
|
|
1530
1542
|
* @param args - Arguments to pass to the function.
|
|
1531
1543
|
* @returns A promise that resolves to `Some<T>` if successful, or `None` if thrown or rejected.
|
|
1532
|
-
*
|
|
1544
|
+
* @since 1.7.0
|
|
1533
1545
|
* @example
|
|
1534
1546
|
* ```ts
|
|
1535
1547
|
* // Function with arguments
|
|
@@ -1569,6 +1581,7 @@ declare function tryAsyncOption<T, Args extends unknown[]>(task: (...args: Args)
|
|
|
1569
1581
|
* @typeParam T - The expected type of the value contained within the `Option`.
|
|
1570
1582
|
* @param o - The value to be checked as an `Option`.
|
|
1571
1583
|
* @returns `true` if the value is an `Option`, otherwise `false`.
|
|
1584
|
+
* @since 1.2.0
|
|
1572
1585
|
* @example
|
|
1573
1586
|
* ```ts
|
|
1574
1587
|
* const x = Some(5);
|
|
@@ -1586,7 +1599,7 @@ declare function isOption<T>(o: unknown): o is Option<T>;
|
|
|
1586
1599
|
* @typeParam T - The type of the value to be wrapped in a `Some`.
|
|
1587
1600
|
* @param value - The value to wrap as a `Some` option.
|
|
1588
1601
|
* @returns An `Option<T>` that contains the provided value, representing the `Some` case.
|
|
1589
|
-
*
|
|
1602
|
+
* @since 1.0.0
|
|
1590
1603
|
* @example
|
|
1591
1604
|
* ```ts
|
|
1592
1605
|
* const maybeValue = Some(1); // Option<number> with a value
|
|
@@ -1641,6 +1654,7 @@ interface None extends Option<never> {
|
|
|
1641
1654
|
* A constant representing the `None` case of an `Option`, indicating the absence of a value.
|
|
1642
1655
|
* This constant is frozen to ensure it is immutable and cannot be altered, preserving the integrity of `None` throughout the application.
|
|
1643
1656
|
*
|
|
1657
|
+
* @since 1.0.0
|
|
1644
1658
|
* @example
|
|
1645
1659
|
* ```ts
|
|
1646
1660
|
* // Use None to represent absence of a value
|
|
@@ -1668,6 +1682,7 @@ declare const None: None;
|
|
|
1668
1682
|
* Since `None extends Option<never>`, this constant can be assigned to any
|
|
1669
1683
|
* `AsyncOption<T>` (i.e., `Promise<Option<T>>`) due to TypeScript's covariance.
|
|
1670
1684
|
*
|
|
1685
|
+
* @since 1.8.0
|
|
1671
1686
|
* @example
|
|
1672
1687
|
* ```ts
|
|
1673
1688
|
* async function findUser(id: number): AsyncOption<User> {
|
|
@@ -1699,7 +1714,7 @@ declare const ASYNC_NONE: Promise<None>;
|
|
|
1699
1714
|
* @typeParam E - The type of the error that the result could potentially contain (not used in this case).
|
|
1700
1715
|
* @param value - The value to wrap as an `Ok` result.
|
|
1701
1716
|
* @returns A `Result<T, E>` that contains the provided value, representing the `Ok` case.
|
|
1702
|
-
*
|
|
1717
|
+
* @since 1.0.0
|
|
1703
1718
|
* @example
|
|
1704
1719
|
* ```ts
|
|
1705
1720
|
* const goodResult = Ok<number, Error>(1); // Result<number, Error> with a value
|
|
@@ -1718,7 +1733,7 @@ declare function Ok<T, E = never>(value: T): Result<T, E>;
|
|
|
1718
1733
|
*
|
|
1719
1734
|
* @typeParam E - The type of the error that the result could potentially contain.
|
|
1720
1735
|
* @returns A `Result<void, E>` representing a successful operation with no value.
|
|
1721
|
-
*
|
|
1736
|
+
* @since 1.4.0
|
|
1722
1737
|
* @example
|
|
1723
1738
|
* ```ts
|
|
1724
1739
|
* function saveToFile(path: string): Result<void, Error> {
|
|
@@ -1745,7 +1760,7 @@ declare function Ok<E = never>(): Result<void, E>;
|
|
|
1745
1760
|
* @typeParam E - The type of the error to be wrapped in the `Err` result.
|
|
1746
1761
|
* @param error - The error to wrap as an `Err` result.
|
|
1747
1762
|
* @returns A `Result<T, E>` that contains the provided error, representing the `Err` case.
|
|
1748
|
-
*
|
|
1763
|
+
* @since 1.0.0
|
|
1749
1764
|
* @example
|
|
1750
1765
|
* ```ts
|
|
1751
1766
|
* const badResult = Err<number, Error>(new Error('Something went wrong'));
|
|
@@ -1771,6 +1786,7 @@ declare function Err<T = never, E = unknown>(error: E): Result<T, E>;
|
|
|
1771
1786
|
* Similar to Rust's `Result<(), E>`.
|
|
1772
1787
|
*
|
|
1773
1788
|
* @typeParam E - The type of the error that may be produced by a failed operation.
|
|
1789
|
+
* @since 1.4.0
|
|
1774
1790
|
* @example
|
|
1775
1791
|
* ```ts
|
|
1776
1792
|
* function saveData(data: string): VoidResult<Error> {
|
|
@@ -1787,6 +1803,7 @@ type VoidResult<E> = Result<void, E>;
|
|
|
1787
1803
|
* `VoidResult<E>` wrapped by `Promise`.
|
|
1788
1804
|
*
|
|
1789
1805
|
* @typeParam E - The type of the error that may be produced by a failed operation.
|
|
1806
|
+
* @since 1.4.0
|
|
1790
1807
|
* @example
|
|
1791
1808
|
* ```ts
|
|
1792
1809
|
* async function deleteFile(path: string): AsyncVoidResult<Error> {
|
|
@@ -1805,6 +1822,7 @@ type AsyncVoidResult<E> = Promise<VoidResult<E>>;
|
|
|
1805
1822
|
* This is a result that is either `Ok(T)` if the operation was successful, or `Err(Error)` if there was an error.
|
|
1806
1823
|
*
|
|
1807
1824
|
* @typeParam T - The type of the value that is produced by a successful operation.
|
|
1825
|
+
* @since 1.0.0
|
|
1808
1826
|
* @example
|
|
1809
1827
|
* ```ts
|
|
1810
1828
|
* function parseJSON<T>(json: string): IOResult<T> {
|
|
@@ -1822,6 +1840,7 @@ type IOResult<T> = Result<T, Error>;
|
|
|
1822
1840
|
* This is a promise that resolves to `Ok(T)` if the I/O operation was successful, or `Err(Error)` if there was an error.
|
|
1823
1841
|
*
|
|
1824
1842
|
* @typeParam T - The type of the value that is produced by a successful I/O operation.
|
|
1843
|
+
* @since 1.0.0
|
|
1825
1844
|
* @example
|
|
1826
1845
|
* ```ts
|
|
1827
1846
|
* async function readFile(path: string): AsyncIOResult<string> {
|
|
@@ -1837,6 +1856,7 @@ type IOResult<T> = Result<T, Error>;
|
|
|
1837
1856
|
type AsyncIOResult<T> = AsyncResult<T, Error>;
|
|
1838
1857
|
/**
|
|
1839
1858
|
* Similar to Rust's `Result<(), Error>`.
|
|
1859
|
+
* @since 1.4.0
|
|
1840
1860
|
* @example
|
|
1841
1861
|
* ```ts
|
|
1842
1862
|
* function logMessage(msg: string): VoidIOResult {
|
|
@@ -1852,6 +1872,7 @@ type AsyncIOResult<T> = AsyncResult<T, Error>;
|
|
|
1852
1872
|
type VoidIOResult = IOResult<void>;
|
|
1853
1873
|
/**
|
|
1854
1874
|
* `VoidIOResult` wrapped by `Promise`.
|
|
1875
|
+
* @since 1.4.0
|
|
1855
1876
|
* @example
|
|
1856
1877
|
* ```ts
|
|
1857
1878
|
* async function sendEmail(to: string, body: string): AsyncVoidIOResult {
|
|
@@ -1874,6 +1895,7 @@ type AsyncVoidIOResult = AsyncIOResult<void>;
|
|
|
1874
1895
|
* - Safe extraction via `intoOk()` without runtime checks
|
|
1875
1896
|
*
|
|
1876
1897
|
* @typeParam T - The type of the value that is always produced.
|
|
1898
|
+
* @since 1.8.0
|
|
1877
1899
|
* @see {@link Result.intoOk}
|
|
1878
1900
|
* @example
|
|
1879
1901
|
* ```ts
|
|
@@ -1889,6 +1911,7 @@ type SafeResult<T> = Result<T, never>;
|
|
|
1889
1911
|
* `SafeResult<T>` wrapped by `Promise`.
|
|
1890
1912
|
*
|
|
1891
1913
|
* @typeParam T - The type of the value that is always produced.
|
|
1914
|
+
* @since 1.8.0
|
|
1892
1915
|
* @example
|
|
1893
1916
|
* ```ts
|
|
1894
1917
|
* async function loadCachedData(): AsyncSafeResult<Data> {
|
|
@@ -1902,6 +1925,7 @@ type AsyncSafeResult<T> = Promise<SafeResult<T>>;
|
|
|
1902
1925
|
/**
|
|
1903
1926
|
* Result constant for `true`.
|
|
1904
1927
|
* Can be used anywhere due to immutability.
|
|
1928
|
+
* @since 1.3.0
|
|
1905
1929
|
* @example
|
|
1906
1930
|
* ```ts
|
|
1907
1931
|
* function validate(): Result<boolean, Error> {
|
|
@@ -1916,6 +1940,7 @@ declare const RESULT_TRUE: Result<boolean, never>;
|
|
|
1916
1940
|
/**
|
|
1917
1941
|
* Result constant for `false`.
|
|
1918
1942
|
* Can be used anywhere due to immutability.
|
|
1943
|
+
* @since 1.3.0
|
|
1919
1944
|
* @example
|
|
1920
1945
|
* ```ts
|
|
1921
1946
|
* function validate(): Result<boolean, Error> {
|
|
@@ -1930,6 +1955,7 @@ declare const RESULT_FALSE: Result<boolean, never>;
|
|
|
1930
1955
|
/**
|
|
1931
1956
|
* Result constant for `0`.
|
|
1932
1957
|
* Can be used anywhere due to immutability.
|
|
1958
|
+
* @since 1.3.0
|
|
1933
1959
|
* @example
|
|
1934
1960
|
* ```ts
|
|
1935
1961
|
* function count(): Result<number, Error> {
|
|
@@ -1944,6 +1970,7 @@ declare const RESULT_ZERO: Result<number, never>;
|
|
|
1944
1970
|
/**
|
|
1945
1971
|
* Result constant for `void` or `()`.
|
|
1946
1972
|
* Can be used anywhere due to immutability.
|
|
1973
|
+
* @since 1.4.0
|
|
1947
1974
|
* @example
|
|
1948
1975
|
* ```ts
|
|
1949
1976
|
* function doSomething(): Result<void, Error> {
|
|
@@ -1970,7 +1997,7 @@ declare const RESULT_VOID: Result<void, never>;
|
|
|
1970
1997
|
* @param fn - A function that may throw an exception.
|
|
1971
1998
|
* @param args - Arguments to pass to the function.
|
|
1972
1999
|
* @returns `Ok<T>` if the function succeeds, or `Err<E>` if it throws.
|
|
1973
|
-
*
|
|
2000
|
+
* @since 1.7.0
|
|
1974
2001
|
* @example
|
|
1975
2002
|
* ```ts
|
|
1976
2003
|
* // Parse JSON safely with arguments
|
|
@@ -2006,7 +2033,7 @@ declare function tryResult<T, E = Error, Args extends unknown[] = []>(fn: (...ar
|
|
|
2006
2033
|
* @typeParam E - The type of the error that may be rejected, defaults to `Error`.
|
|
2007
2034
|
* @param task - A promise or promise-like object to await.
|
|
2008
2035
|
* @returns A promise that resolves to `Ok<T>` if successful, or `Err<E>` if rejected.
|
|
2009
|
-
*
|
|
2036
|
+
* @since 1.7.0
|
|
2010
2037
|
* @example
|
|
2011
2038
|
* ```ts
|
|
2012
2039
|
* // Fetch data safely
|
|
@@ -2037,7 +2064,7 @@ declare function tryAsyncResult<T, E = Error>(task: PromiseLike<T>): AsyncResult
|
|
|
2037
2064
|
* @param task - A function that returns a value or promise-like object.
|
|
2038
2065
|
* @param args - Arguments to pass to the function.
|
|
2039
2066
|
* @returns A promise that resolves to `Ok<T>` if successful, or `Err<E>` if thrown or rejected.
|
|
2040
|
-
*
|
|
2067
|
+
* @since 1.7.0
|
|
2041
2068
|
* @example
|
|
2042
2069
|
* ```ts
|
|
2043
2070
|
* // Function with arguments
|
|
@@ -2084,6 +2111,7 @@ declare function tryAsyncResult<T, E = Error, Args extends unknown[] = []>(task:
|
|
|
2084
2111
|
* @typeParam E - The expected type of the error value contained within the `Result`.
|
|
2085
2112
|
* @param r - The value to be checked as a `Result`.
|
|
2086
2113
|
* @returns `true` if the value is a `Result`, otherwise `false`.
|
|
2114
|
+
* @since 1.2.0
|
|
2087
2115
|
* @example
|
|
2088
2116
|
* ```ts
|
|
2089
2117
|
* const x = Ok(5);
|
|
@@ -2136,8 +2164,8 @@ declare const ControlFlowKindSymbol: unique symbol;
|
|
|
2136
2164
|
*
|
|
2137
2165
|
* @typeParam B - The type of the value returned on `Break` (early exit).
|
|
2138
2166
|
* @typeParam C - The type of the value returned on `Continue` (default: `void`).
|
|
2167
|
+
* @since 1.6.0
|
|
2139
2168
|
* @see https://doc.rust-lang.org/std/ops/enum.ControlFlow.html
|
|
2140
|
-
*
|
|
2141
2169
|
* @example
|
|
2142
2170
|
* ```ts
|
|
2143
2171
|
* // Using ControlFlow to short-circuit a search
|
|
@@ -2235,7 +2263,6 @@ interface ControlFlow<B, C = void> {
|
|
|
2235
2263
|
* `ControlFlow` was `Break` and `None` otherwise.
|
|
2236
2264
|
*
|
|
2237
2265
|
* @returns `Some(value)` if `Break`, `None` if `Continue`.
|
|
2238
|
-
*
|
|
2239
2266
|
* @example
|
|
2240
2267
|
* ```ts
|
|
2241
2268
|
* console.log(Break(3).breakValue()); // Some(3)
|
|
@@ -2248,7 +2275,6 @@ interface ControlFlow<B, C = void> {
|
|
|
2248
2275
|
* `ControlFlow` was `Continue` and `None` otherwise.
|
|
2249
2276
|
*
|
|
2250
2277
|
* @returns `Some(value)` if `Continue`, `None` if `Break`.
|
|
2251
|
-
*
|
|
2252
2278
|
* @example
|
|
2253
2279
|
* ```ts
|
|
2254
2280
|
* console.log(Continue(5).continueValue()); // Some(5)
|
|
@@ -2263,7 +2289,6 @@ interface ControlFlow<B, C = void> {
|
|
|
2263
2289
|
* @typeParam T - The type of the new break value.
|
|
2264
2290
|
* @param fn - A function to apply to the break value.
|
|
2265
2291
|
* @returns A new `ControlFlow` with the mapped break value.
|
|
2266
|
-
*
|
|
2267
2292
|
* @example
|
|
2268
2293
|
* ```ts
|
|
2269
2294
|
* const flow = Break(3);
|
|
@@ -2278,7 +2303,6 @@ interface ControlFlow<B, C = void> {
|
|
|
2278
2303
|
* @typeParam T - The type of the new continue value.
|
|
2279
2304
|
* @param fn - A function to apply to the continue value.
|
|
2280
2305
|
* @returns A new `ControlFlow` with the mapped continue value.
|
|
2281
|
-
*
|
|
2282
2306
|
* @example
|
|
2283
2307
|
* ```ts
|
|
2284
2308
|
* const flow = Continue(5);
|
|
@@ -2291,7 +2315,6 @@ interface ControlFlow<B, C = void> {
|
|
|
2291
2315
|
* `ControlFlow` was `Break` and `Err` otherwise.
|
|
2292
2316
|
*
|
|
2293
2317
|
* @returns `Ok(breakValue)` if `Break`, `Err(continueValue)` if `Continue`.
|
|
2294
|
-
*
|
|
2295
2318
|
* @example
|
|
2296
2319
|
* ```ts
|
|
2297
2320
|
* console.log(Break(3).breakOk()); // Ok(3)
|
|
@@ -2304,7 +2327,6 @@ interface ControlFlow<B, C = void> {
|
|
|
2304
2327
|
* `ControlFlow` was `Continue` and `Err` otherwise.
|
|
2305
2328
|
*
|
|
2306
2329
|
* @returns `Ok(continueValue)` if `Continue`, `Err(breakValue)` if `Break`.
|
|
2307
|
-
*
|
|
2308
2330
|
* @example
|
|
2309
2331
|
* ```ts
|
|
2310
2332
|
* console.log(Continue(5).continueOk()); // Ok(5)
|
|
@@ -2319,7 +2341,6 @@ interface ControlFlow<B, C = void> {
|
|
|
2319
2341
|
* It returns the contained value regardless of whether this is a `Break` or `Continue`.
|
|
2320
2342
|
*
|
|
2321
2343
|
* @returns The contained value.
|
|
2322
|
-
*
|
|
2323
2344
|
* @example
|
|
2324
2345
|
* ```ts
|
|
2325
2346
|
* const breakFlow: ControlFlow<number, number> = Break(5);
|
|
@@ -2340,7 +2361,6 @@ interface ControlFlow<B, C = void> {
|
|
|
2340
2361
|
* @typeParam C - The type of the continue value (defaults to `void` when a value is provided).
|
|
2341
2362
|
* @param value - The value to return on break.
|
|
2342
2363
|
* @returns A `ControlFlow` in the `Break` state.
|
|
2343
|
-
*
|
|
2344
2364
|
* @example
|
|
2345
2365
|
* ```ts
|
|
2346
2366
|
* const flow = Break('found it');
|
|
@@ -2358,7 +2378,6 @@ declare function Break<B, C = never>(value: B): ControlFlow<B, C>;
|
|
|
2358
2378
|
*
|
|
2359
2379
|
* @typeParam C - The type of the continue value (allows type specification when chaining with Continue).
|
|
2360
2380
|
* @returns A `ControlFlow<void, C>` in the `Break` state.
|
|
2361
|
-
*
|
|
2362
2381
|
* @example
|
|
2363
2382
|
* ```ts
|
|
2364
2383
|
* const voidFlow = Break();
|
|
@@ -2379,7 +2398,6 @@ declare function Break<C = never>(): ControlFlow<void, C>;
|
|
|
2379
2398
|
* @typeParam C - The type of the continue value.
|
|
2380
2399
|
* @param value - The value to carry forward (optional, defaults to `undefined`).
|
|
2381
2400
|
* @returns A `ControlFlow` in the `Continue` state.
|
|
2382
|
-
*
|
|
2383
2401
|
* @example
|
|
2384
2402
|
* ```ts
|
|
2385
2403
|
* const flow = Continue();
|
|
@@ -2396,7 +2414,6 @@ declare function Continue<B = never, C = void>(value: C): ControlFlow<B, C>;
|
|
|
2396
2414
|
*
|
|
2397
2415
|
* @typeParam B - The type of the break value (allows type specification when chaining with Break).
|
|
2398
2416
|
* @returns A `ControlFlow<B, void>` in the `Continue` state.
|
|
2399
|
-
*
|
|
2400
2417
|
* @example
|
|
2401
2418
|
* ```ts
|
|
2402
2419
|
* const voidFlow = Continue();
|
|
@@ -2432,10 +2449,9 @@ declare function Continue<B = never>(): ControlFlow<B, void>;
|
|
|
2432
2449
|
*
|
|
2433
2450
|
* @typeParam A - Tuple type of the function arguments.
|
|
2434
2451
|
* @typeParam R - Return type of the function.
|
|
2435
|
-
*
|
|
2452
|
+
* @since 1.8.0
|
|
2436
2453
|
* @see {@link FnOnceAsync} for async one-time callable functions
|
|
2437
2454
|
* @see https://doc.rust-lang.org/std/ops/trait.FnOnce.html
|
|
2438
|
-
*
|
|
2439
2455
|
* @example
|
|
2440
2456
|
* ```ts
|
|
2441
2457
|
* // Basic usage
|
|
@@ -2506,7 +2522,6 @@ interface FnOnce<A extends unknown[], R> {
|
|
|
2506
2522
|
* @param args - The arguments to pass to the function.
|
|
2507
2523
|
* @returns The return value of the function.
|
|
2508
2524
|
* @throws {Error} If the function has already been called.
|
|
2509
|
-
*
|
|
2510
2525
|
* @example
|
|
2511
2526
|
* ```ts
|
|
2512
2527
|
* const add = FnOnce((a: number, b: number) => a + b);
|
|
@@ -2523,7 +2538,6 @@ interface FnOnce<A extends unknown[], R> {
|
|
|
2523
2538
|
*
|
|
2524
2539
|
* @param args - The arguments to pass to the function.
|
|
2525
2540
|
* @returns `Some(result)` if the function was called, `None` if already consumed.
|
|
2526
|
-
*
|
|
2527
2541
|
* @example
|
|
2528
2542
|
* ```ts
|
|
2529
2543
|
* const greet = FnOnce((name: string) => `Hi, ${name}`);
|
|
@@ -2552,7 +2566,6 @@ interface FnOnce<A extends unknown[], R> {
|
|
|
2552
2566
|
* @typeParam R - Return type of the function.
|
|
2553
2567
|
* @param fn - The function to wrap.
|
|
2554
2568
|
* @returns A `FnOnce` instance that wraps the function.
|
|
2555
|
-
*
|
|
2556
2569
|
* @example
|
|
2557
2570
|
* ```ts
|
|
2558
2571
|
* const initialize = FnOnce(() => {
|
|
@@ -2589,9 +2602,8 @@ declare function FnOnce<A extends unknown[], R>(fn: (...args: A) => R): FnOnce<A
|
|
|
2589
2602
|
*
|
|
2590
2603
|
* @typeParam A - Tuple type of the function arguments.
|
|
2591
2604
|
* @typeParam R - The resolved type of the Promise returned by the async function.
|
|
2592
|
-
*
|
|
2605
|
+
* @since 1.8.0
|
|
2593
2606
|
* @see {@link FnOnce} for sync one-time callable functions
|
|
2594
|
-
*
|
|
2595
2607
|
* @example
|
|
2596
2608
|
* ```ts
|
|
2597
2609
|
* // Basic usage
|
|
@@ -2655,7 +2667,6 @@ interface FnOnceAsync<A extends unknown[], R> {
|
|
|
2655
2667
|
* @param args - The arguments to pass to the function.
|
|
2656
2668
|
* @returns A Promise that resolves to the return value of the function.
|
|
2657
2669
|
* @throws {Error} If the function has already been called.
|
|
2658
|
-
*
|
|
2659
2670
|
* @example
|
|
2660
2671
|
* ```ts
|
|
2661
2672
|
* const fetchUser = FnOnceAsync(async (id: number) => {
|
|
@@ -2677,7 +2688,6 @@ interface FnOnceAsync<A extends unknown[], R> {
|
|
|
2677
2688
|
*
|
|
2678
2689
|
* @param args - The arguments to pass to the function.
|
|
2679
2690
|
* @returns A Promise that resolves to `Some(result)` if the function was called, `None` if already consumed.
|
|
2680
|
-
*
|
|
2681
2691
|
* @example
|
|
2682
2692
|
* ```ts
|
|
2683
2693
|
* const fetchData = FnOnceAsync(async (id: number) => {
|
|
@@ -2710,7 +2720,6 @@ interface FnOnceAsync<A extends unknown[], R> {
|
|
|
2710
2720
|
* @typeParam R - The resolved type of the Promise returned by the async function.
|
|
2711
2721
|
* @param fn - A function that returns `PromiseLike<R>` or `R`.
|
|
2712
2722
|
* @returns A `FnOnceAsync` instance that wraps the function.
|
|
2713
|
-
*
|
|
2714
2723
|
* @example
|
|
2715
2724
|
* ```ts
|
|
2716
2725
|
* const initialize = FnOnceAsync(async () => {
|
|
@@ -2739,6 +2748,7 @@ declare function FnOnceAsync<A extends unknown[], R>(fn: (...args: A) => Promise
|
|
|
2739
2748
|
* @typeParam C - The expected type of the continue value contained within the `ControlFlow`.
|
|
2740
2749
|
* @param cf - The value to be checked as a `ControlFlow`.
|
|
2741
2750
|
* @returns `true` if the value is a `ControlFlow`, otherwise `false`.
|
|
2751
|
+
* @since 1.6.0
|
|
2742
2752
|
* @example
|
|
2743
2753
|
* ```ts
|
|
2744
2754
|
* const x = Break(5);
|
|
@@ -2764,7 +2774,6 @@ declare function isControlFlow<B, C>(cf: unknown): cf is ControlFlow<B, C>;
|
|
|
2764
2774
|
* Created by calling `channel.sender()`. Shares state with the parent channel.
|
|
2765
2775
|
*
|
|
2766
2776
|
* @typeParam T - The type of values that can be sent.
|
|
2767
|
-
*
|
|
2768
2777
|
* @see https://doc.rust-lang.org/std/sync/mpmc/struct.Sender.html
|
|
2769
2778
|
*/
|
|
2770
2779
|
interface Sender<T> {
|
|
@@ -2825,9 +2834,7 @@ interface Sender<T> {
|
|
|
2825
2834
|
*
|
|
2826
2835
|
* @param value - The value to send.
|
|
2827
2836
|
* @returns A promise that resolves to `true` if sent successfully, `false` if the channel is closed.
|
|
2828
|
-
*
|
|
2829
2837
|
* @see {@link Channel.send}
|
|
2830
|
-
*
|
|
2831
2838
|
* @example
|
|
2832
2839
|
* ```ts
|
|
2833
2840
|
* const success = await sender.send(42);
|
|
@@ -2846,9 +2853,7 @@ interface Sender<T> {
|
|
|
2846
2853
|
*
|
|
2847
2854
|
* @param value - The value to send.
|
|
2848
2855
|
* @returns `true` if sent successfully, `false` if full or closed.
|
|
2849
|
-
*
|
|
2850
2856
|
* @see {@link Channel.trySend}
|
|
2851
|
-
*
|
|
2852
2857
|
* @example
|
|
2853
2858
|
* ```ts
|
|
2854
2859
|
* if (!sender.trySend(42)) {
|
|
@@ -2869,7 +2874,6 @@ interface Sender<T> {
|
|
|
2869
2874
|
* `false` if timed out, channel is full, or closed.
|
|
2870
2875
|
*
|
|
2871
2876
|
* @see {@link Channel.sendTimeout}
|
|
2872
|
-
*
|
|
2873
2877
|
* @example
|
|
2874
2878
|
* ```ts
|
|
2875
2879
|
* const success = await sender.sendTimeout(42, 1000);
|
|
@@ -2887,7 +2891,6 @@ interface Sender<T> {
|
|
|
2887
2891
|
* Implements `AsyncIterable` for use with `for await...of`.
|
|
2888
2892
|
*
|
|
2889
2893
|
* @typeParam T - The type of values that can be received.
|
|
2890
|
-
*
|
|
2891
2894
|
* @see https://doc.rust-lang.org/std/sync/mpmc/struct.Receiver.html
|
|
2892
2895
|
*/
|
|
2893
2896
|
interface Receiver<T> {
|
|
@@ -2959,9 +2962,7 @@ interface Receiver<T> {
|
|
|
2959
2962
|
* - If the channel is closed and empty, returns `None`.
|
|
2960
2963
|
*
|
|
2961
2964
|
* @returns A promise that resolves to `Some(value)` or `None` if closed and empty.
|
|
2962
|
-
*
|
|
2963
2965
|
* @see {@link Channel.receive}
|
|
2964
|
-
*
|
|
2965
2966
|
* @example
|
|
2966
2967
|
* ```ts
|
|
2967
2968
|
* const result = await receiver.receive();
|
|
@@ -2981,9 +2982,7 @@ interface Receiver<T> {
|
|
|
2981
2982
|
* - Otherwise returns `None`.
|
|
2982
2983
|
*
|
|
2983
2984
|
* @returns `Some(value)` if available, `None` if empty.
|
|
2984
|
-
*
|
|
2985
2985
|
* @see {@link Channel.tryReceive}
|
|
2986
|
-
*
|
|
2987
2986
|
* @example
|
|
2988
2987
|
* ```ts
|
|
2989
2988
|
* const result = receiver.tryReceive();
|
|
@@ -3002,7 +3001,6 @@ interface Receiver<T> {
|
|
|
3002
3001
|
* empty, or closed.
|
|
3003
3002
|
*
|
|
3004
3003
|
* @see {@link Channel.receiveTimeout}
|
|
3005
|
-
*
|
|
3006
3004
|
* @example
|
|
3007
3005
|
* ```ts
|
|
3008
3006
|
* const result = await receiver.receiveTimeout(1000);
|
|
@@ -3020,8 +3018,8 @@ interface Receiver<T> {
|
|
|
3020
3018
|
* typed values. Values are delivered in FIFO order.
|
|
3021
3019
|
*
|
|
3022
3020
|
* @typeParam T - The type of values that can be sent through the channel.
|
|
3021
|
+
* @since 1.8.0
|
|
3023
3022
|
* @see https://doc.rust-lang.org/std/sync/mpmc/fn.channel.html
|
|
3024
|
-
*
|
|
3025
3023
|
* @example
|
|
3026
3024
|
* ```ts
|
|
3027
3025
|
* // Create a bounded channel with capacity 10
|
|
@@ -3198,7 +3196,6 @@ interface Channel<T> {
|
|
|
3198
3196
|
*
|
|
3199
3197
|
* @param value - The value to send.
|
|
3200
3198
|
* @returns A promise that resolves to `true` if sent successfully, `false` if the channel is closed.
|
|
3201
|
-
*
|
|
3202
3199
|
* @example
|
|
3203
3200
|
* ```ts
|
|
3204
3201
|
* const ch = Channel<number>(1);
|
|
@@ -3218,7 +3215,6 @@ interface Channel<T> {
|
|
|
3218
3215
|
*
|
|
3219
3216
|
* @param value - The value to send.
|
|
3220
3217
|
* @returns `true` if sent successfully, `false` if full or closed.
|
|
3221
|
-
*
|
|
3222
3218
|
* @example
|
|
3223
3219
|
* ```ts
|
|
3224
3220
|
* const ch = Channel<number>(0);
|
|
@@ -3257,7 +3253,6 @@ interface Channel<T> {
|
|
|
3257
3253
|
* - If the channel is closed and empty, returns `None`.
|
|
3258
3254
|
*
|
|
3259
3255
|
* @returns A promise that resolves to `Some(value)` or `None` if closed and empty.
|
|
3260
|
-
*
|
|
3261
3256
|
* @example
|
|
3262
3257
|
* ```ts
|
|
3263
3258
|
* const ch = Channel<number>(10);
|
|
@@ -3277,7 +3272,6 @@ interface Channel<T> {
|
|
|
3277
3272
|
* - Otherwise returns `None`.
|
|
3278
3273
|
*
|
|
3279
3274
|
* @returns `Some(value)` if available, `None` if empty.
|
|
3280
|
-
*
|
|
3281
3275
|
* @example
|
|
3282
3276
|
* ```ts
|
|
3283
3277
|
* const ch = Channel<number>(10);
|
|
@@ -3336,7 +3330,7 @@ interface Channel<T> {
|
|
|
3336
3330
|
* @param capacity - Maximum buffer size. Defaults to `Infinity` (unbounded).
|
|
3337
3331
|
* Use `0` for a rendezvous channel (direct handoff).
|
|
3338
3332
|
* @returns A new `Channel<T>` instance.
|
|
3339
|
-
*
|
|
3333
|
+
* @throws {RangeError} If capacity is negative or not an integer (except Infinity).
|
|
3340
3334
|
* @example
|
|
3341
3335
|
* ```ts
|
|
3342
3336
|
* // Unbounded channel (default)
|
|
@@ -3375,9 +3369,9 @@ interface Channel<T> {
|
|
|
3375
3369
|
* // Multiple producers
|
|
3376
3370
|
* async function logFromService(name: string) {
|
|
3377
3371
|
* const sender = logs.sender();
|
|
3378
|
-
* await sender.send(`[${
|
|
3372
|
+
* await sender.send(`[${name}] Started`);
|
|
3379
3373
|
* // ... do work ...
|
|
3380
|
-
* await sender.send(`[${
|
|
3374
|
+
* await sender.send(`[${name}] Finished`);
|
|
3381
3375
|
* }
|
|
3382
3376
|
*
|
|
3383
3377
|
* // Single consumer writing to file
|
|
@@ -3410,10 +3404,9 @@ declare function Channel<T>(capacity?: number): Channel<T>;
|
|
|
3410
3404
|
* on first access. Subsequent accesses return the cached value.
|
|
3411
3405
|
*
|
|
3412
3406
|
* @typeParam T - The type of the value stored.
|
|
3413
|
-
*
|
|
3407
|
+
* @since 1.6.0
|
|
3414
3408
|
* @see {@link LazyAsync} for async lazy initialization
|
|
3415
3409
|
* @see https://doc.rust-lang.org/std/sync/struct.LazyLock.html
|
|
3416
|
-
*
|
|
3417
3410
|
* @example
|
|
3418
3411
|
* ```ts
|
|
3419
3412
|
* const expensive = Lazy(() => {
|
|
@@ -3457,7 +3450,7 @@ interface Lazy<T> {
|
|
|
3457
3450
|
* and returns it.
|
|
3458
3451
|
*
|
|
3459
3452
|
* @returns The initialized value.
|
|
3460
|
-
*
|
|
3453
|
+
* @throws Rethrows any exception thrown by the initialization function.
|
|
3461
3454
|
* @example
|
|
3462
3455
|
* ```ts
|
|
3463
3456
|
* const lazy = Lazy(() => 42);
|
|
@@ -3472,7 +3465,6 @@ interface Lazy<T> {
|
|
|
3472
3465
|
* Unlike `force()`, this does not trigger initialization.
|
|
3473
3466
|
*
|
|
3474
3467
|
* @returns `Some(value)` if initialized, `None` otherwise.
|
|
3475
|
-
*
|
|
3476
3468
|
* @example
|
|
3477
3469
|
* ```ts
|
|
3478
3470
|
* const lazy = Lazy(() => 42);
|
|
@@ -3505,7 +3497,6 @@ interface Lazy<T> {
|
|
|
3505
3497
|
* @typeParam T - The type of value to store.
|
|
3506
3498
|
* @param fn - The initialization function that produces the value.
|
|
3507
3499
|
* @returns A new `Lazy<T>` instance.
|
|
3508
|
-
*
|
|
3509
3500
|
* @example
|
|
3510
3501
|
* ```ts
|
|
3511
3502
|
* // Basic usage
|
|
@@ -3561,9 +3552,8 @@ declare function Lazy<T>(fn: () => T): Lazy<T>;
|
|
|
3561
3552
|
* completes, only one initialization will run.
|
|
3562
3553
|
*
|
|
3563
3554
|
* @typeParam T - The type of the value stored.
|
|
3564
|
-
*
|
|
3555
|
+
* @since 1.6.0
|
|
3565
3556
|
* @see {@link Lazy} for sync-only lazy initialization
|
|
3566
|
-
*
|
|
3567
3557
|
* @example
|
|
3568
3558
|
* ```ts
|
|
3569
3559
|
* const db = LazyAsync(async () => {
|
|
@@ -3605,7 +3595,7 @@ interface LazyAsync<T> {
|
|
|
3605
3595
|
* Otherwise, starts initialization.
|
|
3606
3596
|
*
|
|
3607
3597
|
* @returns A promise that resolves to the initialized value.
|
|
3608
|
-
*
|
|
3598
|
+
* @throws Rejects with any error thrown or rejected by the initialization function.
|
|
3609
3599
|
* @example
|
|
3610
3600
|
* ```ts
|
|
3611
3601
|
* const lazy = LazyAsync(async () => {
|
|
@@ -3622,7 +3612,6 @@ interface LazyAsync<T> {
|
|
|
3622
3612
|
* Unlike `force()`, this does not trigger initialization.
|
|
3623
3613
|
*
|
|
3624
3614
|
* @returns `Some(value)` if initialized, `None` otherwise.
|
|
3625
|
-
*
|
|
3626
3615
|
* @example
|
|
3627
3616
|
* ```ts
|
|
3628
3617
|
* const lazy = LazyAsync(async () => 42);
|
|
@@ -3659,7 +3648,6 @@ interface LazyAsync<T> {
|
|
|
3659
3648
|
* @typeParam T - The type of value to store.
|
|
3660
3649
|
* @param fn - A function that returns `PromiseLike<T>` or `T` to initialize.
|
|
3661
3650
|
* @returns A new `LazyAsync<T>` instance.
|
|
3662
|
-
*
|
|
3663
3651
|
* @example
|
|
3664
3652
|
* ```ts
|
|
3665
3653
|
* // Basic usage
|
|
@@ -3746,7 +3734,6 @@ interface MutexGuard<T> {
|
|
|
3746
3734
|
* Custom `toString` implementation.
|
|
3747
3735
|
*
|
|
3748
3736
|
* @returns A string representation of the guard.
|
|
3749
|
-
*
|
|
3750
3737
|
* @example
|
|
3751
3738
|
* ```ts
|
|
3752
3739
|
* const guard = await mutex.lock();
|
|
@@ -3783,8 +3770,8 @@ interface MutexGuard<T> {
|
|
|
3783
3770
|
* serializes async operations in the single-threaded event loop.
|
|
3784
3771
|
*
|
|
3785
3772
|
* @typeParam T - The type of the protected value.
|
|
3773
|
+
* @since 1.6.0
|
|
3786
3774
|
* @see https://doc.rust-lang.org/std/sync/struct.Mutex.html
|
|
3787
|
-
*
|
|
3788
3775
|
* @example
|
|
3789
3776
|
* ```ts
|
|
3790
3777
|
* const mutex = Mutex({ balance: 100 });
|
|
@@ -3827,7 +3814,6 @@ interface Mutex<T> {
|
|
|
3827
3814
|
* @typeParam U - The return type of the callback.
|
|
3828
3815
|
* @param fn - The callback that receives the protected value.
|
|
3829
3816
|
* @returns A promise that resolves to the callback's return value.
|
|
3830
|
-
*
|
|
3831
3817
|
* @example
|
|
3832
3818
|
* ```ts
|
|
3833
3819
|
* const mutex = Mutex<number[]>([]);
|
|
@@ -3845,7 +3831,6 @@ interface Mutex<T> {
|
|
|
3845
3831
|
* **Important:** Always release the lock in a `finally` block to prevent deadlocks.
|
|
3846
3832
|
*
|
|
3847
3833
|
* @returns A promise that resolves to a guard providing access to the value.
|
|
3848
|
-
*
|
|
3849
3834
|
* @example
|
|
3850
3835
|
* ```ts
|
|
3851
3836
|
* const guard = await mutex.lock();
|
|
@@ -3866,7 +3851,6 @@ interface Mutex<T> {
|
|
|
3866
3851
|
* or `None` if it's currently held by another operation.
|
|
3867
3852
|
*
|
|
3868
3853
|
* @returns `Some(guard)` if acquired, `None` if locked.
|
|
3869
|
-
*
|
|
3870
3854
|
* @example
|
|
3871
3855
|
* ```ts
|
|
3872
3856
|
* const maybeGuard = mutex.tryLock();
|
|
@@ -3904,7 +3888,6 @@ interface Mutex<T> {
|
|
|
3904
3888
|
* This is a convenience method equivalent to `withLock(v => v)`.
|
|
3905
3889
|
*
|
|
3906
3890
|
* @returns A promise that resolves to a copy of the value.
|
|
3907
|
-
*
|
|
3908
3891
|
* @example
|
|
3909
3892
|
* ```ts
|
|
3910
3893
|
* const mutex = Mutex(42);
|
|
@@ -3920,7 +3903,6 @@ interface Mutex<T> {
|
|
|
3920
3903
|
*
|
|
3921
3904
|
* @param value - The new value to set.
|
|
3922
3905
|
* @returns A promise that resolves when the value has been set.
|
|
3923
|
-
*
|
|
3924
3906
|
* @example
|
|
3925
3907
|
* ```ts
|
|
3926
3908
|
* const mutex = Mutex(42);
|
|
@@ -3939,7 +3921,6 @@ interface Mutex<T> {
|
|
|
3939
3921
|
*
|
|
3940
3922
|
* @param value - The new value to set.
|
|
3941
3923
|
* @returns A promise that resolves to the old value.
|
|
3942
|
-
*
|
|
3943
3924
|
* @example
|
|
3944
3925
|
* ```ts
|
|
3945
3926
|
* const mutex = Mutex(42);
|
|
@@ -3956,7 +3937,6 @@ interface Mutex<T> {
|
|
|
3956
3937
|
* @typeParam T - The type of the protected value.
|
|
3957
3938
|
* @param value - The initial value to protect.
|
|
3958
3939
|
* @returns A new `Mutex<T>` instance.
|
|
3959
|
-
*
|
|
3960
3940
|
* @example
|
|
3961
3941
|
* ```ts
|
|
3962
3942
|
* // Protect a simple value
|
|
@@ -4047,10 +4027,9 @@ declare function Mutex<T>(value: T): Mutex<T>;
|
|
|
4047
4027
|
* that should only happen once.
|
|
4048
4028
|
*
|
|
4049
4029
|
* @typeParam T - The type of the value stored.
|
|
4050
|
-
*
|
|
4030
|
+
* @since 1.6.0
|
|
4051
4031
|
* @see {@link OnceAsync} for async one-time initialization
|
|
4052
4032
|
* @see https://doc.rust-lang.org/std/sync/struct.OnceLock.html
|
|
4053
|
-
*
|
|
4054
4033
|
* @example
|
|
4055
4034
|
* ```ts
|
|
4056
4035
|
* const once = Once<number>();
|
|
@@ -4092,7 +4071,6 @@ interface Once<T> {
|
|
|
4092
4071
|
* Gets the reference to the underlying value.
|
|
4093
4072
|
*
|
|
4094
4073
|
* @returns `Some(value)` if initialized, `None` otherwise.
|
|
4095
|
-
*
|
|
4096
4074
|
* @example
|
|
4097
4075
|
* ```ts
|
|
4098
4076
|
* const once = Once<number>();
|
|
@@ -4108,7 +4086,6 @@ interface Once<T> {
|
|
|
4108
4086
|
*
|
|
4109
4087
|
* @param value - The value to store.
|
|
4110
4088
|
* @returns `Ok(undefined)` if empty, `Err(value)` if already initialized.
|
|
4111
|
-
*
|
|
4112
4089
|
* @example
|
|
4113
4090
|
* ```ts
|
|
4114
4091
|
* const once = Once<number>();
|
|
@@ -4127,7 +4104,6 @@ interface Once<T> {
|
|
|
4127
4104
|
*
|
|
4128
4105
|
* @param value - The value to store.
|
|
4129
4106
|
* @returns `Ok(value)` if empty, `Err([currentValue, passedValue])` if already initialized.
|
|
4130
|
-
*
|
|
4131
4107
|
* @example
|
|
4132
4108
|
* ```ts
|
|
4133
4109
|
* const once = Once<number>();
|
|
@@ -4147,7 +4123,6 @@ interface Once<T> {
|
|
|
4147
4123
|
*
|
|
4148
4124
|
* @param fn - The synchronous initialization function, called only if empty.
|
|
4149
4125
|
* @returns The stored value.
|
|
4150
|
-
*
|
|
4151
4126
|
* @example
|
|
4152
4127
|
* ```ts
|
|
4153
4128
|
* const once = Once<number>();
|
|
@@ -4171,7 +4146,6 @@ interface Once<T> {
|
|
|
4171
4146
|
* @typeParam E - The error type.
|
|
4172
4147
|
* @param fn - The initialization function that may fail.
|
|
4173
4148
|
* @returns `Ok(value)` if initialized, `Err(error)` if initialization failed.
|
|
4174
|
-
*
|
|
4175
4149
|
* @example
|
|
4176
4150
|
* ```ts
|
|
4177
4151
|
* const once = Once<Config>();
|
|
@@ -4191,7 +4165,6 @@ interface Once<T> {
|
|
|
4191
4165
|
* Takes the value out, leaving it uninitialized.
|
|
4192
4166
|
*
|
|
4193
4167
|
* @returns `Some(value)` if initialized, `None` otherwise.
|
|
4194
|
-
*
|
|
4195
4168
|
* @example
|
|
4196
4169
|
* ```ts
|
|
4197
4170
|
* const once = Once<number>();
|
|
@@ -4222,7 +4195,6 @@ interface Once<T> {
|
|
|
4222
4195
|
*
|
|
4223
4196
|
* @typeParam T - The type of value to store.
|
|
4224
4197
|
* @returns A new uninitialized `Once`.
|
|
4225
|
-
*
|
|
4226
4198
|
* @example
|
|
4227
4199
|
* ```ts
|
|
4228
4200
|
* // Basic usage
|
|
@@ -4280,9 +4252,8 @@ declare function Once<T>(): Once<T>;
|
|
|
4280
4252
|
* - `OnceAsync<T>` always stores `Awaited<T>` (flattened value)
|
|
4281
4253
|
*
|
|
4282
4254
|
* @typeParam T - The type parameter. The actual stored type is `Awaited<T>`.
|
|
4283
|
-
*
|
|
4255
|
+
* @since 1.8.0
|
|
4284
4256
|
* @see {@link Once} for sync-only one-time initialization
|
|
4285
|
-
*
|
|
4286
4257
|
* @example
|
|
4287
4258
|
* ```ts
|
|
4288
4259
|
* const db = OnceAsync<Database>();
|
|
@@ -4331,7 +4302,6 @@ interface OnceAsync<T> {
|
|
|
4331
4302
|
* Gets the reference to the underlying value.
|
|
4332
4303
|
*
|
|
4333
4304
|
* @returns `Some(value)` if initialized, `None` otherwise.
|
|
4334
|
-
*
|
|
4335
4305
|
* @example
|
|
4336
4306
|
* ```ts
|
|
4337
4307
|
* const once = OnceAsync<number>();
|
|
@@ -4347,7 +4317,6 @@ interface OnceAsync<T> {
|
|
|
4347
4317
|
*
|
|
4348
4318
|
* @param value - The value to store.
|
|
4349
4319
|
* @returns `Ok(undefined)` if empty, `Err(value)` if already initialized.
|
|
4350
|
-
*
|
|
4351
4320
|
* @example
|
|
4352
4321
|
* ```ts
|
|
4353
4322
|
* const once = OnceAsync<number>();
|
|
@@ -4366,7 +4335,6 @@ interface OnceAsync<T> {
|
|
|
4366
4335
|
*
|
|
4367
4336
|
* @param value - The value to store.
|
|
4368
4337
|
* @returns `Ok(value)` if empty, `Err([currentValue, passedValue])` if already initialized.
|
|
4369
|
-
*
|
|
4370
4338
|
* @example
|
|
4371
4339
|
* ```ts
|
|
4372
4340
|
* const once = OnceAsync<number>();
|
|
@@ -4389,7 +4357,6 @@ interface OnceAsync<T> {
|
|
|
4389
4357
|
*
|
|
4390
4358
|
* @param fn - A function that returns `PromiseLike<Awaited<T>>` or `Awaited<T>` to initialize.
|
|
4391
4359
|
* @returns A promise that resolves to the stored value (`Awaited<T>`).
|
|
4392
|
-
*
|
|
4393
4360
|
* @example
|
|
4394
4361
|
* ```ts
|
|
4395
4362
|
* const db = OnceAsync<Database>();
|
|
@@ -4414,7 +4381,6 @@ interface OnceAsync<T> {
|
|
|
4414
4381
|
* @typeParam E - The error type.
|
|
4415
4382
|
* @param fn - A function that returns `PromiseLike<Result<Awaited<T>, E>>` or `Result<Awaited<T>, E>`.
|
|
4416
4383
|
* @returns A promise that resolves to `Ok(value)` or `Err(error)`.
|
|
4417
|
-
*
|
|
4418
4384
|
* @example
|
|
4419
4385
|
* ```ts
|
|
4420
4386
|
* const config = OnceAsync<Config>();
|
|
@@ -4434,7 +4400,6 @@ interface OnceAsync<T> {
|
|
|
4434
4400
|
* Takes the value out, leaving it uninitialized.
|
|
4435
4401
|
*
|
|
4436
4402
|
* @returns `Some(value)` if initialized, `None` otherwise.
|
|
4437
|
-
*
|
|
4438
4403
|
* @example
|
|
4439
4404
|
* ```ts
|
|
4440
4405
|
* const once = OnceAsync<number>();
|
|
@@ -4468,7 +4433,6 @@ interface OnceAsync<T> {
|
|
|
4468
4433
|
* the returned promise will resolve when another caller initializes the cell.
|
|
4469
4434
|
*
|
|
4470
4435
|
* @returns A promise that resolves to the stored value once initialized.
|
|
4471
|
-
*
|
|
4472
4436
|
* @example
|
|
4473
4437
|
* ```ts
|
|
4474
4438
|
* const once = OnceAsync<number>();
|
|
@@ -4493,7 +4457,6 @@ interface OnceAsync<T> {
|
|
|
4493
4457
|
*
|
|
4494
4458
|
* @typeParam T - The type parameter. The actual stored type is `Awaited<T>`.
|
|
4495
4459
|
* @returns A new uninitialized `OnceAsync`.
|
|
4496
|
-
*
|
|
4497
4460
|
* @example
|
|
4498
4461
|
* ```ts
|
|
4499
4462
|
* // Basic usage
|
|
@@ -4588,7 +4551,6 @@ interface RwLockReadGuard<T> {
|
|
|
4588
4551
|
* Custom `toString` implementation.
|
|
4589
4552
|
*
|
|
4590
4553
|
* @returns A string representation of the guard.
|
|
4591
|
-
*
|
|
4592
4554
|
* @example
|
|
4593
4555
|
* ```ts
|
|
4594
4556
|
* const guard = await rwlock.read();
|
|
@@ -4642,7 +4604,6 @@ interface RwLockWriteGuard<T> {
|
|
|
4642
4604
|
* Custom `toString` implementation.
|
|
4643
4605
|
*
|
|
4644
4606
|
* @returns A string representation of the guard.
|
|
4645
|
-
*
|
|
4646
4607
|
* @example
|
|
4647
4608
|
* ```ts
|
|
4648
4609
|
* const guard = await rwlock.write();
|
|
@@ -4674,8 +4635,8 @@ interface RwLockWriteGuard<T> {
|
|
|
4674
4635
|
* Writers are given priority to prevent writer starvation.
|
|
4675
4636
|
*
|
|
4676
4637
|
* @typeParam T - The type of the protected value.
|
|
4638
|
+
* @since 1.8.0
|
|
4677
4639
|
* @see https://doc.rust-lang.org/std/sync/struct.RwLock.html
|
|
4678
|
-
*
|
|
4679
4640
|
* @example
|
|
4680
4641
|
* ```ts
|
|
4681
4642
|
* const config = RwLock({ apiUrl: 'https://api.example.com', timeout: 5000 });
|
|
@@ -4724,7 +4685,6 @@ interface RwLock<T> {
|
|
|
4724
4685
|
* @typeParam U - The return type of the callback.
|
|
4725
4686
|
* @param fn - The callback that receives the protected value (read-only).
|
|
4726
4687
|
* @returns A promise that resolves to the callback's return value.
|
|
4727
|
-
*
|
|
4728
4688
|
* @example
|
|
4729
4689
|
* ```ts
|
|
4730
4690
|
* const data = await rwlock.withRead(async (value) => {
|
|
@@ -4741,7 +4701,6 @@ interface RwLock<T> {
|
|
|
4741
4701
|
* @typeParam U - The return type of the callback.
|
|
4742
4702
|
* @param fn - The callback that receives the protected value (read-write).
|
|
4743
4703
|
* @returns A promise that resolves to the callback's return value.
|
|
4744
|
-
*
|
|
4745
4704
|
* @example
|
|
4746
4705
|
* ```ts
|
|
4747
4706
|
* await rwlock.withWrite(async (value) => {
|
|
@@ -4754,7 +4713,6 @@ interface RwLock<T> {
|
|
|
4754
4713
|
* Acquires a read lock and returns a guard for manual control.
|
|
4755
4714
|
*
|
|
4756
4715
|
* @returns A promise that resolves to a read guard.
|
|
4757
|
-
*
|
|
4758
4716
|
* @example
|
|
4759
4717
|
* ```ts
|
|
4760
4718
|
* const guard = await rwlock.read();
|
|
@@ -4770,7 +4728,6 @@ interface RwLock<T> {
|
|
|
4770
4728
|
* Acquires a write lock and returns a guard for manual control.
|
|
4771
4729
|
*
|
|
4772
4730
|
* @returns A promise that resolves to a write guard.
|
|
4773
|
-
*
|
|
4774
4731
|
* @example
|
|
4775
4732
|
* ```ts
|
|
4776
4733
|
* const guard = await rwlock.write();
|
|
@@ -4788,7 +4745,6 @@ interface RwLock<T> {
|
|
|
4788
4745
|
* Returns `None` if a write lock is currently held.
|
|
4789
4746
|
*
|
|
4790
4747
|
* @returns `Some(guard)` if acquired, `None` if a writer holds the lock.
|
|
4791
|
-
*
|
|
4792
4748
|
* @example
|
|
4793
4749
|
* ```ts
|
|
4794
4750
|
* const maybeGuard = rwlock.tryRead();
|
|
@@ -4809,7 +4765,6 @@ interface RwLock<T> {
|
|
|
4809
4765
|
* Returns `None` if any read or write lock is currently held.
|
|
4810
4766
|
*
|
|
4811
4767
|
* @returns `Some(guard)` if acquired, `None` if any lock is held.
|
|
4812
|
-
*
|
|
4813
4768
|
* @example
|
|
4814
4769
|
* ```ts
|
|
4815
4770
|
* const maybeGuard = rwlock.tryWrite();
|
|
@@ -4850,7 +4805,6 @@ interface RwLock<T> {
|
|
|
4850
4805
|
* Acquires a read lock and returns a copy of the protected value.
|
|
4851
4806
|
*
|
|
4852
4807
|
* @returns A promise that resolves to a copy of the value.
|
|
4853
|
-
*
|
|
4854
4808
|
* @example
|
|
4855
4809
|
* ```ts
|
|
4856
4810
|
* const value = await rwlock.get();
|
|
@@ -4862,7 +4816,6 @@ interface RwLock<T> {
|
|
|
4862
4816
|
*
|
|
4863
4817
|
* @param value - The new value to set.
|
|
4864
4818
|
* @returns A promise that resolves when the value has been set.
|
|
4865
|
-
*
|
|
4866
4819
|
* @example
|
|
4867
4820
|
* ```ts
|
|
4868
4821
|
* await rwlock.set(newValue);
|
|
@@ -4874,7 +4827,6 @@ interface RwLock<T> {
|
|
|
4874
4827
|
*
|
|
4875
4828
|
* @param value - The new value to set.
|
|
4876
4829
|
* @returns A promise that resolves to the old value.
|
|
4877
|
-
*
|
|
4878
4830
|
* @example
|
|
4879
4831
|
* ```ts
|
|
4880
4832
|
* const rwlock = RwLock(42);
|
|
@@ -4891,7 +4843,6 @@ interface RwLock<T> {
|
|
|
4891
4843
|
* @typeParam T - The type of the protected value.
|
|
4892
4844
|
* @param value - The initial value to protect.
|
|
4893
4845
|
* @returns A new `RwLock<T>` instance.
|
|
4894
|
-
*
|
|
4895
4846
|
* @example
|
|
4896
4847
|
* ```ts
|
|
4897
4848
|
* // Basic usage
|