-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrustlike_result.py
More file actions
40 lines (25 loc) · 819 Bytes
/
rustlike_result.py
File metadata and controls
40 lines (25 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from dataclasses import dataclass
from typing import Generic, Literal, NoReturn, TypeAlias, TypeVar
T_co = TypeVar("T_co", covariant=True)
E_co = TypeVar("E_co", covariant=True)
@dataclass
class Ok(Generic[T_co]):
__slots__ = ("_value",)
_value: T_co
def is_err(self) -> Literal[False]:
return False
def unwrap(self) -> T_co:
return self._value
def unwrap_err(self) -> NoReturn:
raise Exception("Unwrapped an err on an Ok value")
@dataclass
class Err(Generic[E_co]):
__slots__ = ("_value",)
_value: E_co
def is_err(self) -> Literal[True]:
return True
def unwrap(self) -> NoReturn:
raise Exception("Unwrapped on an Err value")
def unwrap_err(self) -> E_co:
return self._value
Result: TypeAlias = Ok[T_co] | Err[E_co]