Skip to content

Commit

Permalink
feat: update pyo3 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
shpaker authored Nov 11, 2024
1 parent 69592fd commit 12de74c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[package]
name = "epyxid"
edition = "2021"
version = "0.3.0"
version = "0.3.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "epyxid"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.21.2", features = ["extension-module"] }
pyo3 = { version = "0.22.6", features = ["extension-module"] }
xid = "1.1.1"

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requires-python = ">=3.8"
authors = [
{name = "Aleksandr Shpak", email = "[email protected]"},
]
version = "0.3.0"
version = "0.3.1"
readme = "README.md"
license = { file = "LICENSE.txt" }
keywords = [
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extern crate core;

use pyo3::prelude::{pymodule, wrap_pyfunction, Bound, PyModule, PyResult, Python};
use pyo3::prelude::{
pymodule, wrap_pyfunction, Bound, PyModule, PyModuleMethods, PyResult, Python,
};

use crate::errors::XIDError;
use crate::utils::{xid_create, xid_from_bytes, xid_from_str};
Expand Down
8 changes: 3 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::str::FromStr;

use pyo3::prelude::PyBytesMethods;
use pyo3::types::PyBytes;
use pyo3::{pyfunction, Bound, PyResult};
use pyo3::{pyfunction, PyResult};
use xid::{Id, ParseIdError};

use crate::errors::XIDError;
Expand All @@ -22,8 +20,8 @@ pub fn xid_from_str(s: &str) -> PyResult<XID> {
}

#[pyfunction]
pub fn xid_from_bytes(b: Bound<PyBytes>) -> PyResult<XID> {
match id_from_bytes(b.as_bytes()) {
pub fn xid_from_bytes(b: Vec<u8>) -> PyResult<XID> {
match id_from_bytes(&b) {
Ok(value) => Ok(XID(value)),
Err(error) => Err(XIDError::new_err(error.to_string())),
}
Expand Down
20 changes: 9 additions & 11 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use std::hash::{DefaultHasher, Hash, Hasher};

use crate::utils::{xid_create, xid_from_bytes, xid_from_str};
use std::hash::{DefaultHasher, Hash, Hasher};

use pyo3::types::{PyBytes, PyDateTime, PyString};
use pyo3::types::{PyBytes, PyDateTime};
use pyo3::{pyclass, pymethods, Bound, FromPyObject, PyResult, Python};
use xid::Id;

#[derive(FromPyObject)]
enum XIDReprTypes<'a> {
enum XIDReprTypes {
#[pyo3(transparent, annotation = "str")]
String(&'a PyString),
String(String),
#[pyo3(transparent, annotation = "bytes")]
Bytes(&'a PyBytes),
Bytes(Vec<u8>),
}

#[pyclass]
Expand All @@ -20,14 +19,13 @@ pub struct XID(pub Id);
#[pymethods]
impl XID {
#[new]
fn new<'p>(py: Python<'p>, data: Option<XIDReprTypes>) -> PyResult<XID> {
#[pyo3(signature = (data=None))]
fn py_new(data: Option<XIDReprTypes>) -> PyResult<XID> {
match data {
None => xid_create(),
Some(repr_value) => match repr_value {
XIDReprTypes::String(value) => xid_from_str(value.to_str().unwrap()),
XIDReprTypes::Bytes(value) => {
xid_from_bytes(PyBytes::new_bound(py, value.as_bytes()))
}
XIDReprTypes::String(value) => xid_from_str(value.as_str()),
XIDReprTypes::Bytes(value) => xid_from_bytes(value),
},
}
}
Expand Down

0 comments on commit 12de74c

Please sign in to comment.