Skip to main content

rustshed

Python 3.10 codecov license

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)

Metadata

Release files for rustshed 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rustshed 0.5.0
File Size Uploaded Provenance
rustshed-0.5.0.tar.gz 10.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for rustshed 0.5.0
File Interpreter ABI Platform
rustshed-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.2 kB

Release files / rustshed-0.5.0.tar.gz

Download URL rustshed-0.5.0.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
27bf1da6ee635864e94cdea83b2d514562585c28b5a94b3829e45952b6de4bf9
BLAKE2b-256 checksum
How to use checksums
7663b262eb6219e6a175c55a4e56c13a056d0d1397e4546c34206ac3104667d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.11.0 Linux/5.15.0-1024-azure

Release files / rustshed-0.5.0-py3-none-any.whl

Download URL rustshed-0.5.0-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90545bc8a32fc46cbe93cdddc659c1b51cca0eba65863c90dbc31e0fdd1012c6
BLAKE2b-256 checksum
How to use checksums
174497759d5a1d604aab464f4f90f0f21cc9c801f66fda175616505a14ce50dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.11.0 Linux/5.15.0-1024-azure

Security for rustshed 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page