Rust types in Python.
Project description
rustshed
A collection of Rust types in Python with complete type annotations.
Supported Types
- Option
- Result
Install
pip install rustshed
Examples
Option
The Option type represents an optional value: every Option[T] is either Some[T] and contains a value of type T, or Null (None in Rust), and does not.
from typing import SupportsIndex, TypeVar
from rustshed import Null, Option, Some
T = TypeVar("T")
class SafeList(list[T]):
def get(self, index: SupportsIndex) -> Option[T]:
try:
return Some(self[index])
except IndexError:
return Null
a_list = SafeList([2, 1, 3, 7])
print(a_list.get(1)) # Some(value=1)
print(a_list.get(420)) # Null
Result
The Result is the type used for returning and propagating errors: every Result[T, E] is either Ok[T], representing success and containing a value of type T, or Err[E], representing failure and containing an error of type E.
from rustshed import to_result, Result
@to_result[ValueError]
def parse(x: str) -> int:
return int(x)
def multiply(a: str, b: str) -> Result[int, str]:
# try to parse two strings and multiply them
# map a possible error to str
return parse(a).and_then(lambda n: parse(b).map(lambda m: n * m)).map_err(str)
print(multiply("21", "2")) # Ok(value=42)
print(multiply("2!", "2")) # Err(error="invalid literal for int() with base 10: '2!'")
Rust's question mark (?) operator
The question mark (?) operator in Rust hides some of the boilerplate of propagating errors up the call stack. Implementing this operator in Python would require changes to the language grammar, hence in rustshed it had to be implemented differently.
Q property
The question mark's functionality has been implemented via the Q property (for both Option and Result types) combined with the rustshed.result_shortcut or rustshed.option_shortcut decorator.
from rustshed import Ok, Result, to_result, result_shortcut
@to_result[ValueError]
def parse(x: str) -> int:
return int(x)
# explicit early error return with match statements
def try_to_parse_early_return(a: str, b: str) -> Result[int, ValueError]:
match parse(a):
case Ok(value):
x = value
case err:
return err
match parse(b):
case Ok(value):
y = value
case err:
return err
return Ok(x + y)
# early error return using the Q property
@result_shortcut
def try_to_parse(a: str, b: str) -> Result[int, ValueError]:
x = parse(a).Q
y = parse(b).Q
return Ok(x + y)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rustshed-0.5.0.tar.gz.
File metadata
- Download URL: rustshed-0.5.0.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.11.0 Linux/5.15.0-1024-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27bf1da6ee635864e94cdea83b2d514562585c28b5a94b3829e45952b6de4bf9
|
|
| MD5 |
c82cd22aa163aae2ad9513fc498c8caa
|
|
| BLAKE2b-256 |
7663b262eb6219e6a175c55a4e56c13a056d0d1397e4546c34206ac3104667d1
|
File details
Details for the file rustshed-0.5.0-py3-none-any.whl.
File metadata
- Download URL: rustshed-0.5.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.11.0 Linux/5.15.0-1024-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90545bc8a32fc46cbe93cdddc659c1b51cca0eba65863c90dbc31e0fdd1012c6
|
|
| MD5 |
162806cf37398aacce25c5dcc405ca70
|
|
| BLAKE2b-256 |
174497759d5a1d604aab464f4f90f0f21cc9c801f66fda175616505a14ce50dd
|