Standard Rust core types implementations for Python.
Project description
sain
a dependency-free library which implements a few of Rust's core crates purely in Python.
It offers a few of the core Rust features such as Vec<T>, Result<T, E>, Option<T> and more. See the equivalent type section below.
a few std types are implemented. Check the project documentation
Install
You'll need Python 3.10 or higher.
PyPI
pip install sain
Overview
sain provides a variety of the standard library crates. such as Vec<T> and converter interfaces.
from sain import Option, Result, Ok, Err
from sain.collections import Vec
from sain.collections.buf import Bytes
from sain.convert import Into, TryFrom
from dataclasses import dataclass, field
# some blob of data.
@dataclass
class Blob(Into[Bytes], TryFrom[str, None]):
tag: str
buffer: bytes
# converts this layout into some raw bytes.
def into(self) -> Bytes:
buf = Bytes()
buf.put_bytes(self.tag.encode())
buf.put_bytes(b"-")
buf.put_bytes(self.buffer)
return buf
@classmethod
def try_from(cls, value: str) -> Result[Blob, None]:
# implement a conversion from a string to a blob.
# in case of success, return Ok(layout)
# and in case of failure, return Ok(None)
tag, buffer = value.split(".") # this is an example.
return Ok(Blob(tag, buffer.encode()))
@dataclass
class BlobStore:
buf: Vec[Blob] = field(default_factory=Vec)
# extends the vec from an iterable.
def add(self, *blobs: Blob):
self.buf.extend(blobs)
# finds blob that's tagged with `pattern`
def find(self, pattern: str) -> Option[Blob]:
return self.buf.iter().find(lambda blob: pattern in blob.tag)
# converts the entire buffer into `Bytes`
def into_bytes(self) -> Result[Bytes, None]:
if not self.buf:
return Err(None)
buffer = Bytes()
for blob in self.buf:
buffer.put_bytes(blob.into())
return Ok(buffer)
blobstore = BlobStore()
blobstore.add(Blob("safe", b"Rust"))
# try to convert the string into a Layout.
match Blob.try_from("unsafe.Python"):
case Ok(blob):
blobstore.add(blob) # add it if parsed.
case Err(_):
... # Error parsing the str.
print(blobstore.into_bytes().expect("cannot convert to bytes").to_str())
built-in types
| name in Rust | name in Python | note | restrictions |
|---|---|---|---|
| Option<T>, Some(T), None | Option[T], Some(T), Some(None) | Some(None) has the same layout as None in Rust |
|
| Result<T, E>, Ok(T), Err(E) | Result[T, E], Ok(T), Err(E) | ||
| Vec<T> | Vec[T] | ||
| HashMap<K, V> | HashMap[K, V] | ||
| bytes::Bytes | Bytes | ||
| LazyLock<T> | Lazy[T] | ||
| OnceLock<T> | Once[T] | ||
| Box<T> | Box[T] | this isn't a heap box, See | |
| MaybeUninit<T> | MaybeUninit[T] | they serve the same purpose, but slightly different | |
| &dyn Default | Default[T] | ||
| &dyn Error | Error | ||
| &dyn Iterator<T> | Iterator[T] | ||
| Iter<'a, T> | Iter[T] | collections called by .iter() are built from this type |
|
| iter::once::<T>() | iter.once[T] | ||
| iter::empty::<T>() | iter.empty[T] | ||
| iter::repeat::<T>() | iter.repeat[T] | ||
| cfg!() | cfg() | runtime cfg, not all predictions are supported | |
| #[cfg_attr] | @cfg_attr() | runtime cfg, not all predictions are supported | |
| #[doc] | @doc() | the docs get generated at runtime | |
| todo!() | todo() | ||
| #[deprecated] | @deprecated() | will get removed when it get stabilized in warnings in Python 3.13 |
|
| unimplemented!() | @unimplemented() |
Notes
Since Rust is a compiled language, Whatever predict in cfg and cfg_attr returns False will not compile.
But there's no such thing as this in Python, So RuntimeError will be raised and whatever was predicated will not run.
Project details
Release history Release notifications | RSS feed
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 sain-1.3.0.tar.gz.
File metadata
- Download URL: sain-1.3.0.tar.gz
- Upload date:
- Size: 53.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.8 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2efd4e9915d42e1a1fbe4a7748ce3315ae5637ed03ea64c8077ca67aded33c4b
|
|
| MD5 |
4cfc6de5d74ac83526b0f8d46b39ad23
|
|
| BLAKE2b-256 |
331d4e9b2a23bfe061699bdcee594204d5d3798a94c0b0e2681ada885532383f
|
File details
Details for the file sain-1.3.0-py2.py3-none-any.whl.
File metadata
- Download URL: sain-1.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 75.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.8 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3bc2718017aeb2018e43a388d9a86c3a18039f56f6f766a7cb61f06b5bfb3dc
|
|
| MD5 |
6480c277343c9b8aa3704c64c6cf5a1d
|
|
| BLAKE2b-256 |
e336d6c53e67b08dbcd1df88c9d7921211968690aa8df0b94660953aa4e603f6
|