impl std trait insted custom trait

This commit is contained in:
shenjack 2023-03-26 10:30:38 +08:00
parent dccb4cd68d
commit 5262c7b896
5 changed files with 221 additions and 132 deletions

View File

@ -20,12 +20,43 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ctor"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096"
dependencies = [
"quote",
"syn 1.0.109",
]
[[package]]
name = "ghost"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e77ac7b51b8e6313251737fcef4b1c01a2ea102bde68415b62c0ee9268fec357"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.5",
]
[[package]] [[package]]
name = "indoc" name = "indoc"
version = "1.0.9" version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306" checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306"
[[package]]
name = "inventory"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "498ae1c9c329c7972b917506239b557a60386839192f1cf0ca034f345b65db99"
dependencies = [
"ctor",
"ghost",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.140" version = "0.2.140"
@ -104,6 +135,7 @@ checksum = "06a3d8e8a46ab2738109347433cb7b96dffda2e4a218b03ef27090238886b147"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"indoc", "indoc",
"inventory",
"libc", "libc",
"memoffset", "memoffset",
"parking_lot", "parking_lot",
@ -142,7 +174,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"pyo3-macros-backend", "pyo3-macros-backend",
"quote", "quote",
"syn", "syn 1.0.109",
] ]
[[package]] [[package]]
@ -153,7 +185,7 @@ checksum = "dc1f43d8e30460f36350d18631ccf85ded64c059829208fe680904c65bcd0a4c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 1.0.109",
] ]
[[package]] [[package]]
@ -197,6 +229,17 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "syn"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89c2d1c76a26822187a1fbb5964e3fff108bc208f02e820ab9dac1234f6b388a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]] [[package]]
name = "target-lexicon" name = "target-lexicon"
version = "0.12.6" version = "0.12.6"

View File

@ -14,4 +14,4 @@ codegen-units = 1
[dependencies.pyo3] [dependencies.pyo3]
version = "0.18.1" version = "0.18.1"
features = ["extension-module"] features = ["extension-module", "multiple-pymethods"]

View File

@ -8,6 +8,7 @@
mod math; mod math;
mod sprite; mod sprite;
mod pymath;
use pyo3::prelude::*; use pyo3::prelude::*;

View File

@ -9,6 +9,7 @@
pub mod macros {} pub mod macros {}
pub mod vector { pub mod vector {
use std::ops::{Add, Div, Mul, Sub};
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Vector2 { pub struct Vector2 {
@ -33,10 +34,6 @@ pub mod vector {
pub trait VectorTrait { pub trait VectorTrait {
fn len(&self) -> i8; // use short int to save memory (even if its not going to save a lot) fn len(&self) -> i8; // use short int to save memory (even if its not going to save a lot)
fn add(&self, other: &Self) -> Self;
fn sub(&self, other: &Self) -> Self;
fn mul(&self, other: &Self) -> Self;
fn truediv(&self, other: &Self) -> Self;
fn floordiv(&self, other: &Self) -> Self; fn floordiv(&self, other: &Self) -> Self;
fn abs(&self) -> f64; fn abs(&self) -> f64;
fn neg(&self) -> Self; fn neg(&self) -> Self;
@ -44,27 +41,42 @@ pub mod vector {
fn radd(&self, other: &Self) -> Self; fn radd(&self, other: &Self) -> Self;
} }
impl Add for Vector2 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::new(self.x + other.x, self.y + other.y)
}
}
impl Sub for Vector2 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self::new(self.x - other.x, self.y - other.y)
}
}
impl Mul for Vector2 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self::new(self.x * other.x, self.y * other.y)
}
}
impl Div for Vector2 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self::new(self.x / other.x, self.y / other.y)
}
}
impl VectorTrait for Vector2 { impl VectorTrait for Vector2 {
fn len(&self) -> i8 { fn len(&self) -> i8 {
return 2; return 2;
} }
fn add(&self, other: &Self) -> Self {
Self::new(self.x + other.x, self.y + other.y)
}
fn sub(&self, other: &Self) -> Self {
Self::new(self.x - other.x, self.y - other.y)
}
fn mul(&self, other: &Self) -> Self {
Self::new(self.x * other.x, self.y * other.y)
}
fn truediv(&self, other: &Self) -> Self {
Self::new(self.x / other.x, self.y / other.y)
}
fn floordiv(&self, other: &Self) -> Self { fn floordiv(&self, other: &Self) -> Self {
// 手动模拟python的//运算符 // 手动模拟python的//运算符
Self::new((self.x / other.x).floor(), (self.y / other.y).floor()) Self::new((self.x / other.x).floor(), (self.y / other.y).floor())
@ -96,27 +108,43 @@ pub mod vector {
} }
} }
impl Add for Vector3 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl Sub for Vector3 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
impl Mul for Vector3 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self::new(self.x * rhs.x, self.y * rhs.y, self.z * rhs.z)
}
}
impl Div for Vector3 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self::new(self.x / rhs.x, self.y / rhs.y, self.z / rhs.z)
}
}
impl VectorTrait for Vector3 { impl VectorTrait for Vector3 {
fn len(&self) -> i8 { fn len(&self) -> i8 {
return 3; return 3;
} }
fn add(&self, other: &Self) -> Self {
Self::new(self.x + other.x, self.y + other.y, self.z + other.z)
}
fn sub(&self, other: &Self) -> Self {
Self::new(self.x - other.x, self.y - other.y, self.z - other.z)
}
fn mul(&self, other: &Self) -> Self {
Self::new(self.x * other.x, self.y * other.y, self.z * other.z)
}
fn truediv(&self, other: &Self) -> Self {
Self::new(self.x / other.x, self.y / other.y, self.z / other.z)
}
fn floordiv(&self, other: &Self) -> Self { fn floordiv(&self, other: &Self) -> Self {
// 手动模拟python的//运算符 // 手动模拟python的//运算符
Self::new( Self::new(
@ -158,42 +186,6 @@ pub mod vector {
return 4; return 4;
} }
fn add(&self, other: &Self) -> Self {
Self::new(
self.x + other.x,
self.y + other.y,
self.z + other.z,
self.w + other.w,
)
}
fn sub(&self, other: &Self) -> Self {
Self::new(
self.x - other.x,
self.y - other.y,
self.z - other.z,
self.w - other.w,
)
}
fn mul(&self, other: &Self) -> Self {
Self::new(
self.x * other.x,
self.y * other.y,
self.z * other.z,
self.w * other.w,
)
}
fn truediv(&self, other: &Self) -> Self {
Self::new(
self.x / other.x,
self.y / other.y,
self.z / other.z,
self.w / other.w,
)
}
fn floordiv(&self, other: &Self) -> Self { fn floordiv(&self, other: &Self) -> Self {
// 手动模拟python的//运算符 // 手动模拟python的//运算符
Self::new( Self::new(
@ -242,6 +234,38 @@ pub mod vector {
} }
} }
impl Add for Vector4 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z, self.w + rhs.w)
}
}
impl Sub for Vector4 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z, self.w - rhs.w)
}
}
impl Mul for Vector4 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self::new(self.x * rhs.x, self.y * rhs.y, self.z * rhs.z, self.w * rhs.w)
}
}
impl Div for Vector4 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self::new(self.x / rhs.x, self.y / rhs.y, self.z / rhs.z, self.w / rhs.w)
}
}
impl Vector2 { impl Vector2 {
pub fn new(x: f64, y: f64) -> Self { pub fn new(x: f64, y: f64) -> Self {
Self { x, y } Self { x, y }
@ -279,59 +303,3 @@ pub mod matrix {
pub line4: Vector4, pub line4: Vector4,
} }
} }
pub mod python_class {
use pyo3::prelude::*;
use super::matrix::{Matrix3, Matrix4};
use super::vector::{Vector2, Vector3, Vector4};
#[pyclass(name = "Vector2")]
pub struct PyVector2 {
pub data: Vector2,
}
#[pyclass(name = "Vector3")]
pub struct PyVector3 {
pub data: Vector3,
}
#[pyclass(name = "Vector4")]
pub struct PyVector4 {
pub data: Vector4,
}
#[pyclass(name = "Matrix3")]
pub struct PyMatrix3 {
pub data: Matrix3,
}
#[pyclass(name = "Matrix4")]
pub struct PyMatrix4 {
pub data: Matrix4,
}
pub trait PyCalc {
fn __add__(&self, other: &Self) -> Self;
fn __sub__(&self, other: &Self) -> Self;
fn __mul__(&self, other: &Self) -> Self;
fn __truediv__(&self, other: &Self) -> Self;
fn __floordiv__(&self, other: &Self) -> Self;
fn __abs__(&self) -> f64;
fn __neg__(&self) -> Self;
fn __round__(&self, ndigits: Option<i64>) -> Self;
fn __radd__(&self, other: &PyAny) -> Self;
fn __eq__(&self, other: &Self) -> bool;
fn __ne__(&self, other: &Self) -> bool;
}
#[pymethods]
impl PyVector2 {
#[new]
fn py_new(x: f64, y: f64) -> Self {
return PyVector2 {
data: Vector2::new(x, y),
};
}
}
}

View File

@ -0,0 +1,77 @@
/*
* -------------------------------
* Difficult Rocket
* Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
* All rights reserved
* -------------------------------
*/
pub mod python_class {
use pyo3::prelude::*;
use crate::math::matrix::{Matrix3, Matrix4};
use crate::math::vector::{Vector2, Vector3, Vector4};
#[pyclass(name = "Vector2")]
pub struct PyVector2 {
pub data: Vector2,
}
#[pyclass(name = "Vector3")]
pub struct PyVector3 {
pub data: Vector3,
}
#[pyclass(name = "Vector4")]
pub struct PyVector4 {
pub data: Vector4,
}
#[pyclass(name = "Matrix3")]
pub struct PyMatrix3 {
pub data: Matrix3,
}
#[pyclass(name = "Matrix4")]
pub struct PyMatrix4 {
pub data: Matrix4,
}
#[allow(unused)]
pub trait PyCalc {
fn __add__(&self, other: &Self) -> Self;
fn __sub__(&self, other: &Self) -> Self;
fn __mul__(&self, other: &Self) -> Self;
fn __truediv__(&self, other: &Self) -> Self;
fn __floordiv__(&self, other: &Self) -> Self;
fn __abs__(&self) -> f64;
fn __neg__(&self) -> Self;
fn __round__(&self, ndigits: Option<i64>) -> Self;
fn __radd__(&self, other: &PyAny) -> Self;
fn __eq__(&self, other: &Self) -> bool;
fn __ne__(&self, other: &Self) -> bool;
}
/// 这是一个用来自动给 impl xxx for xxx 的块去掉 trait 部分的宏
/// 用于在为 pyclass 实现
#[pymethods]
impl PyVector2 {
#[new]
fn py_new(x: f64, y: f64) -> Self {
return Self {
data: Vector2::new(x, y),
};
}
}
#[pymethods]
impl PyVector2 {
fn __add__(&self, other: &Self) -> Self {
return Self {
data: self.data + other.data,
};
}
}
}