>>> import lenrs
>>> len("hello")
5
>>> len(["this", "is", "a", "test"])
4
>>> len(1)
Traceback (most recent call last):
File "", line 1, in
TypeError: object of type 'int' has no len()
Slide 22
Slide 22 text
#![feature(proc_macro)]
#![feature(proc_macro_path_invoc)]
extern crate pyo3;
use pyo3::prelude::*;
#[py::modinit(_lenrs)]
fn init(py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "len")]
fn len(py: Python, obj: PyObject) -> PyResult {
if let Ok(s) = obj.extract::(py) {
return Ok(s.len().to_object(py));
}
if let Ok(v) = obj.extract::>(py) {
return Ok(v.len().to_object(py));
}
Err(PyErr::new::("Not supported"))
}
Ok(())
}
Requires nightly Rust
Module name in Python
Function name in Python
Slide 23
Slide 23 text
Write your own Python Extension in Rust!
https://www.youtube.com/watch?v=D9r__qxtRMQ