From cbc96881b142de8cdf490cf9523a6c9cbbe32191 Mon Sep 17 00:00:00 2001 From: yifanwww Date: Mon, 9 Dec 2024 23:15:23 +0800 Subject: [PATCH] refactor(result): add symbol for general way of detecting result --- .../benchmark/src/extra-methods/equal.log | 12 +++++----- packages/benchmark/src/factories/Err.log | 19 +++++++-------- packages/benchmark/src/factories/Ok.log | 23 +++++++++++-------- packages/benchmark/src/methods/and.log | 14 +++++++---- packages/benchmark/src/methods/andThen.log | 22 +++++++++++------- packages/benchmark/src/methods/err.log | 14 +++++------ packages/benchmark/src/methods/expect.log | 2 +- packages/benchmark/src/methods/expectErr.log | 2 +- packages/benchmark/src/methods/inspect.log | 14 +++++++---- packages/benchmark/src/methods/inspectErr.log | 14 +++++++---- packages/benchmark/src/methods/isErr.log | 6 ++--- packages/benchmark/src/methods/isErrAnd.log | 8 +++---- packages/benchmark/src/methods/isOk.log | 6 ++--- packages/benchmark/src/methods/isOkAnd.log | 8 +++---- packages/benchmark/src/methods/map.log | 16 +++++++++---- packages/benchmark/src/methods/mapErr.log | 16 +++++++++---- packages/benchmark/src/methods/mapOr.log | 14 +++++------ packages/benchmark/src/methods/mapOrElse.log | 14 +++++------ packages/benchmark/src/methods/ok.log | 6 ++--- packages/benchmark/src/methods/or.log | 18 +++++++++++---- packages/benchmark/src/methods/orElse.log | 18 +++++++++++---- packages/benchmark/src/methods/unwrap.log | 4 ++-- packages/benchmark/src/methods/unwrapErr.log | 4 ++-- packages/benchmark/src/methods/unwrapOr.log | 14 +++++------ .../benchmark/src/methods/unwrapOrElse.log | 6 ++--- packages/json/src/index.ts | 4 ++-- packages/result/src/RustlikeResult.ts | 19 +++++++-------- packages/result/src/RustlikeResultAsync.ts | 13 +++++++---- packages/result/src/index.ts | 1 + packages/result/src/is.ts | 12 ++++++++++ packages/result/src/symbols.ts | 2 ++ packages/result/src/types.internal.ts | 5 ++++ 32 files changed, 218 insertions(+), 132 deletions(-) create mode 100644 packages/result/src/is.ts create mode 100644 packages/result/src/symbols.ts diff --git a/packages/benchmark/src/extra-methods/equal.log b/packages/benchmark/src/extra-methods/equal.log index 8b23368..2a21145 100644 --- a/packages/benchmark/src/extra-methods/equal.log +++ b/packages/benchmark/src/extra-methods/equal.log @@ -2,9 +2,9 @@ [ true, false ] Loop N: 100,000 -┌─────────┬──────────────────────────────┬──────────────────────┬───────────────────────┬───────────────┬───────────────┬─────────┐ -│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ -├─────────┼──────────────────────────────┼──────────────────────┼───────────────────────┼───────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.equal #1' │ '3112590.68 ± 0.45%' │ '3096500.04 ± 0.12' │ '322 ± 0.31%' │ '323' │ 322 │ -│ 1 │ 'rustresult Result.equal #2' │ '7945796.82 ± 0.21%' │ '7928799.99 ± 100.02' │ '126 ± 0.19%' │ '126' │ 126 │ -└─────────┴──────────────────────────────┴──────────────────────┴───────────────────────┴───────────────┴───────────────┴─────────┘ +┌─────────┬──────────────────────────────┬───────────────────────┬───────────────┬───────────────┬───────────────┬─────────┐ +│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ +├─────────┼──────────────────────────────┼───────────────────────┼───────────────┼───────────────┼───────────────┼─────────┤ +│ 0 │ 'rustresult Result.equal #1' │ '4984674.61 ± 2.00%' │ '4883499.86' │ '203 ± 0.93%' │ '205' │ 201 │ +│ 1 │ 'rustresult Result.equal #2' │ '10140018.18 ± 0.03%' │ '10137099.98' │ '99 ± 0.03%' │ '99' │ 99 │ +└─────────┴──────────────────────────────┴───────────────────────┴───────────────┴───────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/factories/Err.log b/packages/benchmark/src/factories/Err.log index 81c7a59..4788f7f 100644 --- a/packages/benchmark/src/factories/Err.log +++ b/packages/benchmark/src/factories/Err.log @@ -2,7 +2,8 @@ RustlikeResult { _type: 'err', _value: undefined, - _error: 'error message' + _error: 'error message', + _symbol: Symbol(rust_result) } > neverthrow err: @@ -23,11 +24,11 @@ Err { error: 'error message' } } Loop N: 100,000 -┌─────────┬──────────────────────┬──────────────────────┬──────────────┬────────────────┬───────────────┬─────────┐ -│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ -├─────────┼──────────────────────┼──────────────────────┼──────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Err' │ '396911.70 ± 0.56%' │ '373600.01' │ '2566 ± 0.50%' │ '2677' │ 2520 │ -│ 1 │ 'neverthrow err' │ '272838.43 ± 0.53%' │ '252300.02' │ '3751 ± 0.46%' │ '3964' │ 3666 │ -│ 2 │ 'effect Effect.fail' │ '1199478.54 ± 0.99%' │ '1157300.00' │ '845 ± 0.68%' │ '864' │ 834 │ -│ 3 │ 'effect Exit.fail' │ '1149108.72 ± 0.57%' │ '1125799.89' │ '876 ± 0.52%' │ '888' │ 871 │ -└─────────┴──────────────────────┴──────────────────────┴──────────────┴────────────────┴───────────────┴─────────┘ +┌─────────┬──────────────────────┬──────────────────────┬───────────────────────┬────────────────┬───────────────┬─────────┐ +│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ +├─────────┼──────────────────────┼──────────────────────┼───────────────────────┼────────────────┼───────────────┼─────────┤ +│ 0 │ 'rustresult Err' │ '459830.16 ± 0.59%' │ '442600.01' │ '2213 ± 0.53%' │ '2259' │ 2175 │ +│ 1 │ 'neverthrow err' │ '273295.22 ± 0.56%' │ '252999.90 ± 0.12' │ '3753 ± 0.48%' │ '3953' │ 3660 │ +│ 2 │ 'effect Effect.fail' │ '1179306.25 ± 0.62%' │ '1150750.04 ± 49.95' │ '854 ± 0.55%' │ '869' │ 848 │ +│ 3 │ 'effect Exit.fail' │ '1169452.22 ± 0.67%' │ '1134950.04 ± 349.88' │ '863 ± 0.60%' │ '881' │ 856 │ +└─────────┴──────────────────────┴──────────────────────┴───────────────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/factories/Ok.log b/packages/benchmark/src/factories/Ok.log index 4d32475..556ac10 100644 --- a/packages/benchmark/src/factories/Ok.log +++ b/packages/benchmark/src/factories/Ok.log @@ -1,5 +1,10 @@ > rustresult Ok: -RustlikeResult { _type: 'ok', _value: 1, _error: undefined } +RustlikeResult { + _type: 'ok', + _value: 1, + _error: undefined, + _symbol: Symbol(rust_result) +} > neverthrow ok: Ok { value: 1 } @@ -11,11 +16,11 @@ Ok { value: 1 } { _id: 'Exit', _tag: 'Success', value: 1 } Loop N: 100,000 -┌─────────┬─────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐ -│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ -├─────────┼─────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Ok' │ '405076.67 ± 0.54%' │ '381500.01' │ '2511 ± 0.49%' │ '2621' │ 2469 │ -│ 1 │ 'neverthrow ok' │ '273384.09 ± 0.56%' │ '253700.02' │ '3752 ± 0.48%' │ '3942' │ 3658 │ -│ 2 │ 'effect Effect.succeed' │ '698827.05 ± 0.52%' │ '700500.01' │ '1445 ± 0.49%' │ '1428' │ 1431 │ -│ 3 │ 'effect Exit.succeed' │ '694094.11 ± 0.52%' │ '696500.06' │ '1454 ± 0.49%' │ '1436' │ 1441 │ -└─────────┴─────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘ +┌─────────┬─────────────────────────┬─────────────────────┬─────────────────────┬────────────────┬───────────────┬─────────┐ +│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ +├─────────┼─────────────────────────┼─────────────────────┼─────────────────────┼────────────────┼───────────────┼─────────┤ +│ 0 │ 'rustresult Ok' │ '464204.64 ± 0.65%' │ '442699.91' │ '2197 ± 0.55%' │ '2259' │ 2155 │ +│ 1 │ 'neverthrow ok' │ '284503.50 ± 0.74%' │ '261500.12' │ '3637 ± 0.54%' │ '3824' │ 3515 │ +│ 2 │ 'effect Effect.succeed' │ '714974.62 ± 0.61%' │ '706799.98' │ '1416 ± 0.56%' │ '1415' │ 1399 │ +│ 3 │ 'effect Exit.succeed' │ '711262.45 ± 0.62%' │ '706349.97 ± 49.95' │ '1424 ± 0.56%' │ '1416' │ 1406 │ +└─────────┴─────────────────────────┴─────────────────────┴─────────────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/and.log b/packages/benchmark/src/methods/and.log index 88c7b1a..bb77a2f 100644 --- a/packages/benchmark/src/methods/and.log +++ b/packages/benchmark/src/methods/and.log @@ -1,10 +1,16 @@ > rustresult Result.and: [ - RustlikeResult { _type: 'ok', _value: 100, _error: undefined }, + RustlikeResult { + _type: 'ok', + _value: 100, + _error: undefined, + _symbol: Symbol(rust_result) + }, RustlikeResult { _type: 'err', _value: undefined, - _error: 'error message' + _error: 'error message', + _symbol: Symbol(rust_result) } ] @@ -15,6 +21,6 @@ Loop N: 100,000 ┌─────────┬─────────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼─────────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.and' │ '291635.03 ± 0.36%' │ '277100.09' │ '3459 ± 0.29%' │ '3609' │ 3429 │ -│ 1 │ 'neverthrow Result.and sim' │ '194534.53 ± 0.30%' │ '202000.14' │ '5209 ± 0.33%' │ '4950' │ 5141 │ +│ 0 │ 'rustresult Result.and' │ '318988.00 ± 0.38%' │ '320499.90' │ '3168 ± 0.35%' │ '3120' │ 3135 │ +│ 1 │ 'neverthrow Result.and sim' │ '194788.65 ± 0.32%' │ '201800.11' │ '5207 ± 0.34%' │ '4955' │ 5134 │ └─────────┴─────────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/andThen.log b/packages/benchmark/src/methods/andThen.log index 9be64ce..a0aadad 100644 --- a/packages/benchmark/src/methods/andThen.log +++ b/packages/benchmark/src/methods/andThen.log @@ -1,10 +1,16 @@ > rustresult Result.andThen: [ - RustlikeResult { _type: 'ok', _value: 2, _error: undefined }, + RustlikeResult { + _type: 'ok', + _value: 2, + _error: undefined, + _symbol: Symbol(rust_result) + }, RustlikeResult { _type: 'err', _value: undefined, - _error: 'error message' + _error: 'error message', + _symbol: Symbol(rust_result) } ] @@ -12,9 +18,9 @@ [ Ok { value: 2 }, Err { error: 'error message' } ] Loop N: 100,000 -┌─────────┬─────────────────────────────┬─────────────────────┬─────────────────────┬────────────────┬───────────────┬─────────┐ -│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ -├─────────┼─────────────────────────────┼─────────────────────┼─────────────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.andThen' │ '688856.68 ± 0.14%' │ '682300.09' │ '1453 ± 0.12%' │ '1466' │ 1452 │ -│ 1 │ 'neverthrow Result.andThen' │ '460866.04 ± 0.34%' │ '446949.96 ± 49.83' │ '2180 ± 0.27%' │ '2237' │ 2170 │ -└─────────┴─────────────────────────────┴─────────────────────┴─────────────────────┴────────────────┴───────────────┴─────────┘ +┌─────────┬─────────────────────────────┬─────────────────────┬──────────────────────┬────────────────┬───────────────┬─────────┐ +│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ +├─────────┼─────────────────────────────┼─────────────────────┼──────────────────────┼────────────────┼───────────────┼─────────┤ +│ 0 │ 'rustresult Result.andThen' │ '738685.68 ± 0.19%' │ '729449.99 ± 149.97' │ '1355 ± 0.17%' │ '1371' │ 1354 │ +│ 1 │ 'neverthrow Result.andThen' │ '466934.17 ± 0.34%' │ '452500.10' │ '2152 ± 0.28%' │ '2210' │ 2142 │ +└─────────┴─────────────────────────────┴─────────────────────┴──────────────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/err.log b/packages/benchmark/src/methods/err.log index 4257762..40cca7c 100644 --- a/packages/benchmark/src/methods/err.log +++ b/packages/benchmark/src/methods/err.log @@ -8,10 +8,10 @@ [ undefined, 'error message' ] Loop N: 100,000 -┌─────────┬─────────────────────────────┬──────────────────────┬───────────────────────┬─────────────────┬───────────────┬─────────┐ -│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ -├─────────┼─────────────────────────────┼──────────────────────┼───────────────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.err' │ '24746.12 ± 0.09%' │ '24499.89' │ '40601 ± 0.05%' │ '40817' │ 40411 │ -│ 1 │ 'neverthrow Result.err sim' │ '73818.62 ± 0.04%' │ '73799.85' │ '13552 ± 0.03%' │ '13550' │ 13547 │ -│ 2 │ 'effect Exit.err sim' │ '1941183.71 ± 0.67%' │ '1904399.99 ± 100.02' │ '518 ± 0.61%' │ '525' │ 516 │ -└─────────┴─────────────────────────────┴──────────────────────┴───────────────────────┴─────────────────┴───────────────┴─────────┘ +┌─────────┬─────────────────────────────┬──────────────────────┬──────────────┬─────────────────┬───────────────┬─────────┐ +│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ +├─────────┼─────────────────────────────┼──────────────────────┼──────────────┼─────────────────┼───────────────┼─────────┤ +│ 0 │ 'rustresult Result.err' │ '24853.18 ± 0.10%' │ '24500.13' │ '40474 ± 0.06%' │ '40816' │ 40237 │ +│ 1 │ 'neverthrow Result.err sim' │ '74011.99 ± 0.07%' │ '73800.09' │ '13525 ± 0.04%' │ '13550' │ 13512 │ +│ 2 │ 'effect Exit.err sim' │ '2005453.91 ± 1.06%' │ '1934299.95' │ '504 ± 0.87%' │ '517' │ 499 │ +└─────────┴─────────────────────────────┴──────────────────────┴──────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/expect.log b/packages/benchmark/src/methods/expect.log index 301cc64..0fcea84 100644 --- a/packages/benchmark/src/methods/expect.log +++ b/packages/benchmark/src/methods/expect.log @@ -5,5 +5,5 @@ Loop N: 100,000 ┌─────────┬────────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼────────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.expect' │ '148267.80 ± 0.04%' │ '147600.17' │ '6746 ± 0.03%' │ '6775' │ 6745 │ +│ 0 │ 'rustresult Result.expect' │ '149033.52 ± 0.14%' │ '148499.97' │ '6723 ± 0.08%' │ '6734' │ 6710 │ └─────────┴────────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/expectErr.log b/packages/benchmark/src/methods/expectErr.log index 502a005..1f59c50 100644 --- a/packages/benchmark/src/methods/expectErr.log +++ b/packages/benchmark/src/methods/expectErr.log @@ -5,5 +5,5 @@ Loop N: 100,000 ┌─────────┬───────────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼───────────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.expectErr' │ '148171.54 ± 0.06%' │ '147600.17' │ '6751 ± 0.04%' │ '6775' │ 6749 │ +│ 0 │ 'rustresult Result.expectErr' │ '148469.11 ± 0.05%' │ '148499.97' │ '6737 ± 0.03%' │ '6734' │ 6736 │ └─────────┴───────────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/inspect.log b/packages/benchmark/src/methods/inspect.log index c7471d5..2938576 100644 --- a/packages/benchmark/src/methods/inspect.log +++ b/packages/benchmark/src/methods/inspect.log @@ -1,10 +1,16 @@ > rustresult Result.inspect: [ - RustlikeResult { _type: 'ok', _value: 1, _error: undefined }, + RustlikeResult { + _type: 'ok', + _value: 1, + _error: undefined, + _symbol: Symbol(rust_result) + }, RustlikeResult { _type: 'err', _value: undefined, - _error: 'error message' + _error: 'error message', + _symbol: Symbol(rust_result) } ] @@ -15,6 +21,6 @@ Loop N: 100,000 ┌─────────┬─────────────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼─────────────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.inspect' │ '448032.06 ± 0.32%' │ '462300.06' │ '2243 ± 0.28%' │ '2163' │ 2233 │ -│ 1 │ 'neverthrow Result.inspect sim' │ '505858.47 ± 0.20%' │ '497699.98' │ '1980 ± 0.17%' │ '2009' │ 1977 │ +│ 0 │ 'rustresult Result.inspect' │ '448539.01 ± 0.32%' │ '463700.06' │ '2241 ± 0.29%' │ '2157' │ 2230 │ +│ 1 │ 'neverthrow Result.inspect sim' │ '510191.38 ± 0.20%' │ '500899.79' │ '1964 ± 0.18%' │ '1996' │ 1961 │ └─────────┴─────────────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/inspectErr.log b/packages/benchmark/src/methods/inspectErr.log index 2c0328f..78d4788 100644 --- a/packages/benchmark/src/methods/inspectErr.log +++ b/packages/benchmark/src/methods/inspectErr.log @@ -1,10 +1,16 @@ > rustresult Result.inspectErr: [ - RustlikeResult { _type: 'ok', _value: 1, _error: undefined }, + RustlikeResult { + _type: 'ok', + _value: 1, + _error: undefined, + _symbol: Symbol(rust_result) + }, RustlikeResult { _type: 'err', _value: undefined, - _error: 'error message' + _error: 'error message', + _symbol: Symbol(rust_result) } ] @@ -15,6 +21,6 @@ Loop N: 100,000 ┌─────────┬─────────────────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼─────────────────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.inspectErr' │ '442733.91 ± 0.34%' │ '458999.87' │ '2271 ± 0.30%' │ '2179' │ 2259 │ -│ 1 │ 'neverthrow Resulit.inspectErr sim' │ '480115.84 ± 0.25%' │ '467999.94' │ '2089 ± 0.22%' │ '2137' │ 2083 │ +│ 0 │ 'rustresult Result.inspectErr' │ '447131.78 ± 0.33%' │ '461600.07' │ '2249 ± 0.30%' │ '2166' │ 2237 │ +│ 1 │ 'neverthrow Resulit.inspectErr sim' │ '496411.07 ± 0.81%' │ '473400.12' │ '2047 ± 0.41%' │ '2112' │ 2015 │ └─────────┴─────────────────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/isErr.log b/packages/benchmark/src/methods/isErr.log index 7fa25dc..42bb3e7 100644 --- a/packages/benchmark/src/methods/isErr.log +++ b/packages/benchmark/src/methods/isErr.log @@ -11,7 +11,7 @@ Loop N: 100,000 ┌─────────┬───────────────────────────┬────────────────────┬─────────────┬─────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼───────────────────────────┼────────────────────┼─────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.isErr' │ '24809.33 ± 0.10%' │ '24499.89' │ '40538 ± 0.05%' │ '40817' │ 40308 │ -│ 1 │ 'neverthrow Result.isErr' │ '24574.72 ± 0.05%' │ '24499.89' │ '40745 ± 0.03%' │ '40817' │ 40693 │ -│ 2 │ 'effect Exit.isFailure' │ '74189.05 ± 0.03%' │ '73999.88' │ '13482 ± 0.02%' │ '13514' │ 13480 │ +│ 0 │ 'rustresult Result.isErr' │ '24840.61 ± 0.13%' │ '24500.13' │ '40498 ± 0.05%' │ '40816' │ 40257 │ +│ 1 │ 'neverthrow Result.isErr' │ '24731.36 ± 0.10%' │ '24499.89' │ '40609 ± 0.04%' │ '40817' │ 40435 │ +│ 2 │ 'effect Exit.isFailure' │ '74368.60 ± 0.04%' │ '74299.81' │ '13452 ± 0.03%' │ '13459' │ 13447 │ └─────────┴───────────────────────────┴────────────────────┴─────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/isErrAnd.log b/packages/benchmark/src/methods/isErrAnd.log index 8ff7d6a..7476c45 100644 --- a/packages/benchmark/src/methods/isErrAnd.log +++ b/packages/benchmark/src/methods/isErrAnd.log @@ -14,8 +14,8 @@ Loop N: 100,000 ┌─────────┬──────────────────────────────────┬─────────────────────┬─────────────┬─────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼──────────────────────────────────┼─────────────────────┼─────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.isErrAnd' │ '74118.48 ± 0.11%' │ '73799.85' │ '13524 ± 0.06%' │ '13550' │ 13492 │ -│ 1 │ 'rustresult Result.isErrAnd sim' │ '24693.15 ± 0.08%' │ '24499.89' │ '40650 ± 0.04%' │ '40817' │ 40498 │ -│ 2 │ 'neverthrow Result.isErrAnd sim' │ '24649.20 ± 0.08%' │ '24499.89' │ '40700 ± 0.04%' │ '40817' │ 40570 │ -│ 3 │ 'effect Exit.isErrAnd sim' │ '272338.88 ± 0.02%' │ '272199.87' │ '3672 ± 0.02%' │ '3674' │ 3672 │ +│ 0 │ 'rustresult Result.isErrAnd' │ '73906.27 ± 0.07%' │ '73800.09' │ '13537 ± 0.03%' │ '13550' │ 13531 │ +│ 1 │ 'rustresult Result.isErrAnd sim' │ '25269.90 ± 0.17%' │ '24500.13' │ '40133 ± 0.08%' │ '40816' │ 39573 │ +│ 2 │ 'neverthrow Result.isErrAnd sim' │ '24782.09 ± 0.11%' │ '24499.89' │ '40583 ± 0.05%' │ '40817' │ 40352 │ +│ 3 │ 'effect Exit.isErrAnd sim' │ '272351.57 ± 0.02%' │ '272199.87' │ '3672 ± 0.02%' │ '3674' │ 3672 │ └─────────┴──────────────────────────────────┴─────────────────────┴─────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/isOk.log b/packages/benchmark/src/methods/isOk.log index b635662..0c5a87f 100644 --- a/packages/benchmark/src/methods/isOk.log +++ b/packages/benchmark/src/methods/isOk.log @@ -11,7 +11,7 @@ Loop N: 100,000 ┌─────────┬──────────────────────────┬────────────────────┬─────────────┬─────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼──────────────────────────┼────────────────────┼─────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.isOk' │ '24523.07 ± 0.03%' │ '24499.89' │ '40807 ± 0.02%' │ '40817' │ 40779 │ -│ 1 │ 'neverthrow Result.isOk' │ '24546.63 ± 0.05%' │ '24499.89' │ '40795 ± 0.03%' │ '40817' │ 40739 │ -│ 2 │ 'effect Exit.isSuccess' │ '74842.39 ± 0.15%' │ '73899.98' │ '13421 ± 0.09%' │ '13532' │ 13362 │ +│ 0 │ 'rustresult Result.isOk' │ '24536.97 ± 0.02%' │ '24499.89' │ '40772 ± 0.02%' │ '40817' │ 40755 │ +│ 1 │ 'neverthrow Result.isOk' │ '24516.53 ± 0.04%' │ '24499.89' │ '40817 ± 0.02%' │ '40817' │ 40789 │ +│ 2 │ 'effect Exit.isSuccess' │ '74234.79 ± 0.03%' │ '74199.91' │ '13474 ± 0.02%' │ '13477' │ 13471 │ └─────────┴──────────────────────────┴────────────────────┴─────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/isOkAnd.log b/packages/benchmark/src/methods/isOkAnd.log index 72af36e..54e3cfb 100644 --- a/packages/benchmark/src/methods/isOkAnd.log +++ b/packages/benchmark/src/methods/isOkAnd.log @@ -14,8 +14,8 @@ Loop N: 100,000 ┌─────────┬─────────────────────────────────┬────────────────────┬─────────────┬─────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼─────────────────────────────────┼────────────────────┼─────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.isOkAnd' │ '75528.23 ± 0.21%' │ '73799.85' │ '13357 ± 0.12%' │ '13550' │ 13241 │ -│ 1 │ 'rustresult Result.isOkAnd sim' │ '24506.83 ± 0.03%' │ '24499.89' │ '40828 ± 0.02%' │ '40817' │ 40805 │ -│ 2 │ 'neverthrow Result.isOkAnd sim' │ '24504.43 ± 0.03%' │ '24499.89' │ '40830 ± 0.02%' │ '40817' │ 40809 │ -│ 3 │ 'effect Exit.isOkAnd sim' │ '86445.33 ± 0.03%' │ '86100.10' │ '11571 ± 0.03%' │ '11614' │ 11569 │ +│ 0 │ 'rustresult Result.isOkAnd' │ '73875.47 ± 0.05%' │ '73799.85' │ '13543 ± 0.03%' │ '13550' │ 13537 │ +│ 1 │ 'rustresult Result.isOkAnd sim' │ '24580.50 ± 0.05%' │ '24499.89' │ '40747 ± 0.03%' │ '40817' │ 40683 │ +│ 2 │ 'neverthrow Result.isOkAnd sim' │ '24694.60 ± 0.09%' │ '24499.89' │ '40669 ± 0.05%' │ '40817' │ 40495 │ +│ 3 │ 'effect Exit.isOkAnd sim' │ '86500.92 ± 0.03%' │ '86200.00' │ '11563 ± 0.02%' │ '11601' │ 11561 │ └─────────┴─────────────────────────────────┴────────────────────┴─────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/map.log b/packages/benchmark/src/methods/map.log index 7649fdc..63984a9 100644 --- a/packages/benchmark/src/methods/map.log +++ b/packages/benchmark/src/methods/map.log @@ -1,10 +1,16 @@ > rustresult Result.map: [ - RustlikeResult { _type: 'ok', _value: 2, _error: undefined }, + RustlikeResult { + _type: 'ok', + _value: 2, + _error: undefined, + _symbol: Symbol(rust_result) + }, RustlikeResult { _type: 'err', _value: undefined, - _error: 'error message' + _error: 'error message', + _symbol: Symbol(rust_result) } ] @@ -25,7 +31,7 @@ Loop N: 100,000 ┌─────────┬─────────────────────────┬──────────────────────┬──────────────┬────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼─────────────────────────┼──────────────────────┼──────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.map' │ '428085.91 ± 0.51%' │ '403200.15' │ '2370 ± 0.46%' │ '2480' │ 2336 │ -│ 1 │ 'neverthrow Result.map' │ '260789.83 ± 0.54%' │ '238499.88' │ '3929 ± 0.45%' │ '4193' │ 3835 │ -│ 2 │ 'effect Exit.map' │ '1929968.59 ± 1.57%' │ '1869100.09' │ '526 ± 0.81%' │ '535' │ 519 │ +│ 0 │ 'rustresult Result.map' │ '492947.02 ± 0.57%' │ '475300.07' │ '2060 ± 0.52%' │ '2104' │ 2029 │ +│ 1 │ 'neverthrow Result.map' │ '258763.59 ± 0.55%' │ '236599.92' │ '3964 ± 0.46%' │ '4227' │ 3865 │ +│ 2 │ 'effect Exit.map' │ '1844429.83 ± 0.61%' │ '1808799.98' │ '545 ± 0.54%' │ '553' │ 543 │ └─────────┴─────────────────────────┴──────────────────────┴──────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/mapErr.log b/packages/benchmark/src/methods/mapErr.log index 68d1ce5..43248f2 100644 --- a/packages/benchmark/src/methods/mapErr.log +++ b/packages/benchmark/src/methods/mapErr.log @@ -1,10 +1,16 @@ > rustresult Result.mapErr: [ - RustlikeResult { _type: 'ok', _value: 200, _error: undefined }, + RustlikeResult { + _type: 'ok', + _value: 200, + _error: undefined, + _symbol: Symbol(rust_result) + }, RustlikeResult { _type: 'err', _value: undefined, - _error: 'error code: 400' + _error: 'error code: 400', + _symbol: Symbol(rust_result) } ] @@ -25,7 +31,7 @@ Loop N: 100,000 ┌─────────┬────────────────────────────┬───────────────────────┬─────────────────────────┬────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼────────────────────────────┼───────────────────────┼─────────────────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.mapErr' │ '420016.63 ± 0.51%' │ '394299.98' │ '2416 ± 0.46%' │ '2536' │ 2381 │ -│ 1 │ 'neverthrow Result.mapErr' │ '259376.76 ± 0.54%' │ '237200.02' │ '3951 ± 0.45%' │ '4216' │ 3856 │ -│ 2 │ 'effect Exit.mapError' │ '16362601.58 ± 0.80%' │ '16219249.96 ± 4549.98' │ '61 ± 0.77%' │ '62' │ 64 │ +│ 0 │ 'rustresult Result.mapErr' │ '497488.31 ± 0.51%' │ '478699.92' │ '2035 ± 0.47%' │ '2089' │ 2011 │ +│ 1 │ 'neverthrow Result.mapErr' │ '263314.93 ± 0.55%' │ '241449.95 ± 49.95' │ '3896 ± 0.47%' │ '4142 ± 1' │ 3798 │ +│ 2 │ 'effect Exit.mapError' │ '16449276.57 ± 1.40%' │ '16216900.11 ± 6400.11' │ '61 ± 1.20%' │ '62' │ 64 │ └─────────┴────────────────────────────┴───────────────────────┴─────────────────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/mapOr.log b/packages/benchmark/src/methods/mapOr.log index 9b2780b..60071c7 100644 --- a/packages/benchmark/src/methods/mapOr.log +++ b/packages/benchmark/src/methods/mapOr.log @@ -8,10 +8,10 @@ [ 3, 40 ] Loop N: 100,000 -┌─────────┬───────────────────────────────┬──────────────────────┬────────────────────────┬─────────────────┬───────────────┬─────────┐ -│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ -├─────────┼───────────────────────────────┼──────────────────────┼────────────────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.mapOr' │ '73759.68 ± 0.03%' │ '73799.85' │ '13561 ± 0.02%' │ '13550' │ 13558 │ -│ 1 │ 'neverthrow Result.mapOr sim' │ '73561.45 ± 0.02%' │ '73400.02' │ '13596 ± 0.02%' │ '13624' │ 13595 │ -│ 2 │ 'effect Exit.mapOr sim' │ '1895834.28 ± 1.19%' │ '1826099.99 ± 1299.98' │ '535 ± 0.90%' │ '548' │ 528 │ -└─────────┴───────────────────────────────┴──────────────────────┴────────────────────────┴─────────────────┴───────────────┴─────────┘ +┌─────────┬───────────────────────────────┬──────────────────────┬──────────────┬─────────────────┬───────────────┬─────────┐ +│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ +├─────────┼───────────────────────────────┼──────────────────────┼──────────────┼─────────────────┼───────────────┼─────────┤ +│ 0 │ 'rustresult Result.mapOr' │ '73723.63 ± 0.03%' │ '73400.02' │ '13568 ± 0.03%' │ '13624' │ 13565 │ +│ 1 │ 'neverthrow Result.mapOr sim' │ '73781.26 ± 0.04%' │ '73400.02' │ '13559 ± 0.03%' │ '13624' │ 13554 │ +│ 2 │ 'effect Exit.mapOr sim' │ '1898144.41 ± 0.83%' │ '1855900.05' │ '531 ± 0.74%' │ '539' │ 527 │ +└─────────┴───────────────────────────────┴──────────────────────┴──────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/mapOrElse.log b/packages/benchmark/src/methods/mapOrElse.log index 7783412..4f200fc 100644 --- a/packages/benchmark/src/methods/mapOrElse.log +++ b/packages/benchmark/src/methods/mapOrElse.log @@ -8,10 +8,10 @@ [ 2, 3 ] Loop N: 100,000 -┌─────────┬───────────────────────────────┬──────────────────────┬───────────────────────┬─────────────────┬───────────────┬─────────┐ -│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ -├─────────┼───────────────────────────────┼──────────────────────┼───────────────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.mapOrElse' │ '73725.17 ± 0.04%' │ '73400.02' │ '13569 ± 0.03%' │ '13624' │ 13564 │ -│ 1 │ 'neverthrow Result.match' │ '73764.11 ± 0.06%' │ '73400.02' │ '13567 ± 0.04%' │ '13624' │ 13557 │ -│ 2 │ 'effect Exit.match' │ '1901238.59 ± 0.54%' │ '1883049.96 ± 649.93' │ '528 ± 0.49%' │ '531' │ 526 │ -└─────────┴───────────────────────────────┴──────────────────────┴───────────────────────┴─────────────────┴───────────────┴─────────┘ +┌─────────┬───────────────────────────────┬──────────────────────┬──────────────┬─────────────────┬───────────────┬─────────┐ +│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ +├─────────┼───────────────────────────────┼──────────────────────┼──────────────┼─────────────────┼───────────────┼─────────┤ +│ 0 │ 'rustresult Result.mapOrElse' │ '73779.77 ± 0.03%' │ '73799.85' │ '13557 ± 0.02%' │ '13550' │ 13554 │ +│ 1 │ 'neverthrow Result.match' │ '73898.07 ± 0.05%' │ '73799.85' │ '13540 ± 0.03%' │ '13550' │ 13533 │ +│ 2 │ 'effect Exit.match' │ '1974738.46 ± 0.72%' │ '1936999.80' │ '509 ± 0.63%' │ '516' │ 507 │ +└─────────┴───────────────────────────────┴──────────────────────┴──────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/ok.log b/packages/benchmark/src/methods/ok.log index 4f0a6fd..5d361c9 100644 --- a/packages/benchmark/src/methods/ok.log +++ b/packages/benchmark/src/methods/ok.log @@ -11,7 +11,7 @@ Loop N: 100,000 ┌─────────┬────────────────────────────┬─────────────────────┬─────────────┬─────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼────────────────────────────┼─────────────────────┼─────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.ok' │ '24743.44 ± 0.11%' │ '24499.89' │ '40643 ± 0.05%' │ '40817' │ 40415 │ -│ 1 │ 'neverthrow Result.ok sim' │ '24436.43 ± 0.02%' │ '24400.00' │ '40939 ± 0.02%' │ '40984' │ 40923 │ -│ 2 │ 'effect Exit.ok sim' │ '699580.63 ± 0.38%' │ '669800.04' │ '1437 ± 0.36%' │ '1493' │ 1430 │ +│ 0 │ 'rustresult Result.ok' │ '24806.17 ± 0.11%' │ '24499.89' │ '40554 ± 0.05%' │ '40817' │ 40313 │ +│ 1 │ 'neverthrow Result.ok sim' │ '24767.46 ± 0.68%' │ '24499.89' │ '40701 ± 0.05%' │ '40817' │ 40376 │ +│ 2 │ 'effect Exit.ok sim' │ '700238.00 ± 0.39%' │ '671299.93' │ '1436 ± 0.37%' │ '1490' │ 1429 │ └─────────┴────────────────────────────┴─────────────────────┴─────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/or.log b/packages/benchmark/src/methods/or.log index ecbe1e7..cc292f7 100644 --- a/packages/benchmark/src/methods/or.log +++ b/packages/benchmark/src/methods/or.log @@ -1,7 +1,17 @@ > rustresult Result.or: [ - RustlikeResult { _type: 'ok', _value: 1, _error: undefined }, - RustlikeResult { _type: 'ok', _value: 100, _error: undefined } + RustlikeResult { + _type: 'ok', + _value: 1, + _error: undefined, + _symbol: Symbol(rust_result) + }, + RustlikeResult { + _type: 'ok', + _value: 100, + _error: undefined, + _symbol: Symbol(rust_result) + } ] > neverthrow Result.or simulation: @@ -11,6 +21,6 @@ Loop N: 100,000 ┌─────────┬────────────────────────────┬─────────────────────┬─────────────┬─────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼────────────────────────────┼─────────────────────┼─────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.or' │ '123230.96 ± 0.05%' │ '122999.91' │ '8117 ± 0.03%' │ '8130' │ 8115 │ -│ 1 │ 'neverthrow Result.or sim' │ '73671.24 ± 0.05%' │ '73400.02' │ '13580 ± 0.03%' │ '13624' │ 13574 │ +│ 0 │ 'rustresult Result.or' │ '123387.34 ± 0.05%' │ '122999.91' │ '8108 ± 0.04%' │ '8130' │ 8105 │ +│ 1 │ 'neverthrow Result.or sim' │ '73839.09 ± 0.04%' │ '73799.85' │ '13549 ± 0.03%' │ '13550' │ 13543 │ └─────────┴────────────────────────────┴─────────────────────┴─────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/orElse.log b/packages/benchmark/src/methods/orElse.log index a3f7a83..b38c28c 100644 --- a/packages/benchmark/src/methods/orElse.log +++ b/packages/benchmark/src/methods/orElse.log @@ -1,7 +1,17 @@ > rustresult Result.orElse: [ - RustlikeResult { _type: 'ok', _value: 2, _error: undefined }, - RustlikeResult { _type: 'ok', _value: 6, _error: undefined } + RustlikeResult { + _type: 'ok', + _value: 2, + _error: undefined, + _symbol: Symbol(rust_result) + }, + RustlikeResult { + _type: 'ok', + _value: 6, + _error: undefined, + _symbol: Symbol(rust_result) + } ] > neverthrow Result.orElse: @@ -11,6 +21,6 @@ Loop N: 100,000 ┌─────────┬────────────────────────────┬─────────────────────┬─────────────┬────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼────────────────────────────┼─────────────────────┼─────────────┼────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.orElse' │ '688843.46 ± 0.13%' │ '682699.92' │ '1452 ± 0.11%' │ '1465' │ 1452 │ -│ 1 │ 'neverthrow Result.orElse' │ '462796.95 ± 0.43%' │ '447100.16' │ '2176 ± 0.31%' │ '2237' │ 2161 │ +│ 0 │ 'rustresult Result.orElse' │ '736201.10 ± 0.17%' │ '729799.99' │ '1360 ± 0.16%' │ '1370' │ 1359 │ +│ 1 │ 'neverthrow Result.orElse' │ '472135.92 ± 0.56%' │ '453400.14' │ '2141 ± 0.37%' │ '2206' │ 2119 │ └─────────┴────────────────────────────┴─────────────────────┴─────────────┴────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/unwrap.log b/packages/benchmark/src/methods/unwrap.log index 79f1d63..43e94a9 100644 --- a/packages/benchmark/src/methods/unwrap.log +++ b/packages/benchmark/src/methods/unwrap.log @@ -8,6 +8,6 @@ Loop N: 100,000 ┌─────────┬─────────────────────────────────────┬─────────────────────┬─────────────┬─────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼─────────────────────────────────────┼─────────────────────┼─────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.unwrap' │ '174072.01 ± 0.03%' │ '173300.03' │ '5746 ± 0.03%' │ '5770' │ 5745 │ -│ 1 │ 'rustresult Result.unwrapUnchecked' │ '24550.54 ± 0.03%' │ '24499.89' │ '40765 ± 0.02%' │ '40817' │ 40733 │ +│ 0 │ 'rustresult Result.unwrap' │ '174187.18 ± 0.04%' │ '174299.96' │ '5742 ± 0.03%' │ '5737' │ 5741 │ +│ 1 │ 'rustresult Result.unwrapUnchecked' │ '24688.24 ± 0.08%' │ '24499.89' │ '40664 ± 0.04%' │ '40817' │ 40506 │ └─────────┴─────────────────────────────────────┴─────────────────────┴─────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/unwrapErr.log b/packages/benchmark/src/methods/unwrapErr.log index d02f856..0caf009 100644 --- a/packages/benchmark/src/methods/unwrapErr.log +++ b/packages/benchmark/src/methods/unwrapErr.log @@ -8,6 +8,6 @@ Loop N: 100,000 ┌─────────┬─────────────────────────────────────┬─────────────────────┬─────────────┬─────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼─────────────────────────────────────┼─────────────────────┼─────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Err.unwrapErr' │ '174160.03 ± 0.06%' │ '173300.03' │ '5744 ± 0.05%' │ '5770' │ 5742 │ -│ 1 │ 'rustresult Err.unwrapErrUnchecked' │ '24561.97 ± 0.05%' │ '24499.89' │ '40769 ± 0.03%' │ '40817' │ 40714 │ +│ 0 │ 'rustresult Err.unwrapErr' │ '173983.07 ± 0.04%' │ '173300.03' │ '5749 ± 0.03%' │ '5770' │ 5748 │ +│ 1 │ 'rustresult Err.unwrapErrUnchecked' │ '24599.59 ± 0.06%' │ '24499.89' │ '40727 ± 0.03%' │ '40817' │ 40652 │ └─────────┴─────────────────────────────────────┴─────────────────────┴─────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/unwrapOr.log b/packages/benchmark/src/methods/unwrapOr.log index e7cd205..61765a5 100644 --- a/packages/benchmark/src/methods/unwrapOr.log +++ b/packages/benchmark/src/methods/unwrapOr.log @@ -8,10 +8,10 @@ [ 1, 100 ] Loop N: 100,000 -┌─────────┬──────────────────────────────┬──────────────────────┬──────────────┬─────────────────┬───────────────┬─────────┐ -│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ -├─────────┼──────────────────────────────┼──────────────────────┼──────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.unwrapOr' │ '24538.43 ± 0.04%' │ '24499.89' │ '40794 ± 0.03%' │ '40817' │ 40753 │ -│ 1 │ 'neverthrow Result.unwrapOr' │ '24851.53 ± 0.14%' │ '24499.89' │ '40520 ± 0.06%' │ '40817' │ 40239 │ -│ 2 │ 'effect Exit.unwrapOr sim' │ '1885104.33 ± 0.77%' │ '1849800.11' │ '534 ± 0.70%' │ '541' │ 531 │ -└─────────┴──────────────────────────────┴──────────────────────┴──────────────┴─────────────────┴───────────────┴─────────┘ +┌─────────┬──────────────────────────────┬──────────────────────┬───────────────────────┬─────────────────┬───────────────┬─────────┐ +│ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ +├─────────┼──────────────────────────────┼──────────────────────┼───────────────────────┼─────────────────┼───────────────┼─────────┤ +│ 0 │ 'rustresult Result.unwrapOr' │ '25085.53 ± 0.23%' │ '24499.89' │ '40345 ± 0.07%' │ '40817' │ 39864 │ +│ 1 │ 'neverthrow Result.unwrapOr' │ '24634.16 ± 0.08%' │ '24499.89' │ '40732 ± 0.04%' │ '40817' │ 40595 │ +│ 2 │ 'effect Exit.unwrapOr sim' │ '1819124.73 ± 0.88%' │ '1766150.00 ± 149.97' │ '554 ± 0.71%' │ '566' │ 550 │ +└─────────┴──────────────────────────────┴──────────────────────┴───────────────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/benchmark/src/methods/unwrapOrElse.log b/packages/benchmark/src/methods/unwrapOrElse.log index 410922c..610104a 100644 --- a/packages/benchmark/src/methods/unwrapOrElse.log +++ b/packages/benchmark/src/methods/unwrapOrElse.log @@ -11,7 +11,7 @@ Loop N: 100,000 ┌─────────┬──────────────────────────────────────┬──────────────────────┬──────────────┬─────────────────┬───────────────┬─────────┐ │ (index) │ task │ mean (ns) │ median (ns) │ mean (op/s) │ median (op/s) │ samples │ ├─────────┼──────────────────────────────────────┼──────────────────────┼──────────────┼─────────────────┼───────────────┼─────────┤ -│ 0 │ 'rustresult Result.unwrapOrElse' │ '74972.78 ± 0.23%' │ '73400.02' │ '13454 ± 0.11%' │ '13624' │ 13339 │ -│ 1 │ 'neverthrow Result.unwrapOrElse sim' │ '73818.34 ± 0.03%' │ '73799.85' │ '13551 ± 0.03%' │ '13550' │ 13547 │ -│ 2 │ 'effect Exit.unwrapOrElse sim' │ '1944165.25 ± 0.65%' │ '1908800.13' │ '517 ± 0.60%' │ '524' │ 515 │ +│ 0 │ 'rustresult Result.unwrapOrElse' │ '73832.37 ± 0.03%' │ '73799.85' │ '13549 ± 0.03%' │ '13550' │ 13545 │ +│ 1 │ 'neverthrow Result.unwrapOrElse sim' │ '73813.55 ± 0.04%' │ '73799.85' │ '13553 ± 0.03%' │ '13550' │ 13548 │ +│ 2 │ 'effect Exit.unwrapOrElse sim' │ '1899796.97 ± 0.56%' │ '1872799.87' │ '528 ± 0.52%' │ '534' │ 527 │ └─────────┴──────────────────────────────────────┴──────────────────────┴──────────────┴─────────────────┴───────────────┴─────────┘ diff --git a/packages/json/src/index.ts b/packages/json/src/index.ts index c5770db..99eb74b 100644 --- a/packages/json/src/index.ts +++ b/packages/json/src/index.ts @@ -1,11 +1,11 @@ import type { Result, ResultJson } from '@rustresult/result'; -import { Err, Ok, RustlikeResult } from '@rustresult/result'; +import { Err, Ok, isResult } from '@rustresult/result'; /** * Converts a `Result` to a JSON object. */ function toJSON(result: unknown): unknown { - if (result instanceof RustlikeResult) { + if (isResult(result)) { return result.isOk() ? { type: 'ok', value: toJSON(result.unwrapUnchecked()) } : { type: 'err', value: toJSON(result.unwrapErrUnchecked()) }; diff --git a/packages/result/src/RustlikeResult.ts b/packages/result/src/RustlikeResult.ts index 3a3c62d..0881abc 100644 --- a/packages/result/src/RustlikeResult.ts +++ b/packages/result/src/RustlikeResult.ts @@ -1,7 +1,9 @@ +import { isResult } from './is'; import type { Result } from './Result'; import type { ResultAsync } from './ResultAsync'; // eslint-disable-next-line import/no-cycle import { RustlikeResultAsync } from './RustlikeResultAsync'; +import { RESULT_SYMBOL } from './symbols'; import type { Optional, ResultType } from './types.internal'; /** @@ -11,6 +13,7 @@ export class RustlikeResult implements Result { private readonly _type: ResultType; private readonly _value?: T; private readonly _error?: E; + private readonly _symbol: symbol; constructor(type: 'ok', value: T); constructor(type: 'err', error: E); @@ -23,6 +26,7 @@ export class RustlikeResult implements Result { this._value = undefined; this._error = value as E; } + this._symbol = RESULT_SYMBOL; } /** @@ -159,18 +163,15 @@ export class RustlikeResult implements Result { } private static _equal(self: unknown, other: unknown): boolean { - const isSelfResult = self instanceof RustlikeResult; - const isOtherResult = other instanceof RustlikeResult; + const isSelfResult = isResult(self); + const isOtherResult = isResult(other); if (isSelfResult && isOtherResult) { - const _self: Result = self; - const _other: Result = other; - - const isOk = _self.isOk(); - if (isOk !== _other.isOk()) return false; + const isOk = self.isOk(); + if (isOk !== other.isOk()) return false; return isOk - ? RustlikeResult._equal(_self.unwrapUnchecked(), _other.unwrapUnchecked()) - : RustlikeResult._equal(_self.unwrapErrUnchecked(), _other.unwrapErrUnchecked()); + ? RustlikeResult._equal(self.unwrapUnchecked(), other.unwrapUnchecked()) + : RustlikeResult._equal(self.unwrapErrUnchecked(), other.unwrapErrUnchecked()); } return self === other || (Number.isNaN(self) && Number.isNaN(other)); diff --git a/packages/result/src/RustlikeResultAsync.ts b/packages/result/src/RustlikeResultAsync.ts index 7bdb88a..ff20f31 100644 --- a/packages/result/src/RustlikeResultAsync.ts +++ b/packages/result/src/RustlikeResultAsync.ts @@ -1,15 +1,18 @@ // eslint-disable-next-line import/no-cycle import { Err, Ok } from './factory'; +import { isResult, isResultAsync } from './is'; import type { Result } from './Result'; import type { ResultAsync } from './ResultAsync'; -import { RustlikeResult } from './RustlikeResult'; +import { RESULT_ASYNC_SYMBOL } from './symbols'; import type { Optional } from './types.internal'; export class RustlikeResultAsync implements ResultAsync { private readonly _promise: Promise>; + private readonly _symbol: symbol; constructor(promise: Result | Promise> | ResultAsync) { this._promise = Promise.resolve(promise); + this._symbol = RESULT_ASYNC_SYMBOL; } // async type predicate issue: https://github.com/microsoft/TypeScript/issues/37681 @@ -155,12 +158,12 @@ export class RustlikeResultAsync implements ResultAsync { } private static async _equal(self: unknown, other: unknown): Promise { - const isSelfResult = self instanceof RustlikeResult || self instanceof RustlikeResultAsync; - const isOtherResult = other instanceof RustlikeResult || other instanceof RustlikeResultAsync; + const isSelfResult = isResult(self) || isResultAsync(self); + const isOtherResult = isResult(other) || isResultAsync(other); if (isSelfResult && isOtherResult) { - const _self: Result = await self; - const _other: Result = await other; + const _self = await self; + const _other = await other; const isOk = _self.isOk(); if (isOk !== _other.isOk()) return false; diff --git a/packages/result/src/index.ts b/packages/result/src/index.ts index bf599a6..bd48976 100644 --- a/packages/result/src/index.ts +++ b/packages/result/src/index.ts @@ -1,5 +1,6 @@ export * from './factory'; export * from './factoryAsync'; +export * from './is'; export type * from './Result'; export type * from './ResultAsync'; export * from './resultify'; diff --git a/packages/result/src/is.ts b/packages/result/src/is.ts new file mode 100644 index 0000000..3065aee --- /dev/null +++ b/packages/result/src/is.ts @@ -0,0 +1,12 @@ +import type { Result } from './Result'; +import type { ResultAsync } from './ResultAsync'; +import { RESULT_ASYNC_SYMBOL, RESULT_SYMBOL } from './symbols'; +import type { InternalResult } from './types.internal'; + +export function isResult(value: unknown): value is Result { + return value !== undefined && value !== null && (value as InternalResult)._symbol === RESULT_SYMBOL; +} + +export function isResultAsync(value: unknown): value is ResultAsync { + return value !== undefined && value !== null && (value as InternalResult)._symbol === RESULT_ASYNC_SYMBOL; +} diff --git a/packages/result/src/symbols.ts b/packages/result/src/symbols.ts new file mode 100644 index 0000000..2e8923b --- /dev/null +++ b/packages/result/src/symbols.ts @@ -0,0 +1,2 @@ +export const RESULT_SYMBOL = Symbol.for('rust_result'); +export const RESULT_ASYNC_SYMBOL = Symbol.for('rust_result_async'); diff --git a/packages/result/src/types.internal.ts b/packages/result/src/types.internal.ts index 76e546f..4c3b761 100644 --- a/packages/result/src/types.internal.ts +++ b/packages/result/src/types.internal.ts @@ -16,3 +16,8 @@ export type ResultType = 'ok' | 'err'; * People just simply write `T | undefined`. */ export type Optional = T | undefined; + +export interface InternalResult { + // eslint-disable-next-line @typescript-eslint/naming-convention + _symbol: symbol; +}