From cc5840bef402547399833de704dda027a4440ce3 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 20 Jan 2023 17:41:43 +0800 Subject: [PATCH] \pyo3/ nice rust --- libs/DR-rs/Difficult_Rocket_rs/__init__.pyi | 9 +++++++++ libs/DR-rs/setup.py | 10 ++++++---- libs/DR-rs/src/lib.rs | 13 ++++++++++++- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 libs/DR-rs/Difficult_Rocket_rs/__init__.pyi diff --git a/libs/DR-rs/Difficult_Rocket_rs/__init__.pyi b/libs/DR-rs/Difficult_Rocket_rs/__init__.pyi new file mode 100644 index 0000000..a778b88 --- /dev/null +++ b/libs/DR-rs/Difficult_Rocket_rs/__init__.pyi @@ -0,0 +1,9 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021-2022 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- + +def sum_as_string(a: int, b: int) -> int: ... + +def for_x_in_range(a: int, b: int) -> None: ... diff --git a/libs/DR-rs/setup.py b/libs/DR-rs/setup.py index bb30ebc..a042091 100644 --- a/libs/DR-rs/setup.py +++ b/libs/DR-rs/setup.py @@ -3,14 +3,16 @@ # Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com # All rights reserved # ------------------------------- - +import sys from setuptools import setup from setuptools_rust import Binding, RustExtension setup( - name='Difficult_Rocket-rs', + name='Difficult_Rocket_rs', version='0.0.0.1', - rust_extensions=[RustExtension("Difficult_Rocket-rs.Difficult_Rocket-rs", binding=Binding.PyO3)], - packages=['Difficult_Rocket-rs'], + rust_extensions=[RustExtension("Difficult_Rocket_rs.Difficult_Rocket_rs", binding=Binding.PyO3)], + packages=['Difficult_Rocket_rs'], zip_safe=False ) + +print(sys.platform) diff --git a/libs/DR-rs/src/lib.rs b/libs/DR-rs/src/lib.rs index 92adf16..4bbc282 100644 --- a/libs/DR-rs/src/lib.rs +++ b/libs/DR-rs/src/lib.rs @@ -6,11 +6,22 @@ fn sum_as_string(a: usize, b: usize) -> PyResult { Ok((a + b).to_string()) } +#[pyfunction] +fn for_x_in_range(a: usize, b: usize) -> PyResult<()> { + assert!(a <= b); + for x in a..b { + println!("{}", x); + } + Ok(()) +} + /// A Python module implemented in Rust. The name of this function must match /// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to /// import the module. #[pymodule] -fn string_sum(_py: Python<'_>, m: &PyModule) -> PyResult<()> { +#[allow(non_snake_case)] +fn Difficult_Rocket_rs(_py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + m.add_function(wrap_pyfunction!(for_x_in_range, m)?)?; Ok(()) } \ No newline at end of file