1
- use pyo3:: prelude:: * ;
1
+ use pyo3:: prelude:: pyclass;
2
+ use pyo3:: prelude:: pymethods;
3
+ use pyo3:: prelude:: Bound ;
4
+ use pyo3:: prelude:: Py ;
5
+ use pyo3:: prelude:: PyAny ;
6
+ use pyo3:: prelude:: PyErr ;
7
+ use pyo3:: prelude:: PyObject ;
8
+ use pyo3:: prelude:: PyResult ;
9
+ use pyo3:: prelude:: Python ;
10
+ use pyo3:: types:: PyBytes ;
11
+ use pyo3:: types:: PyModuleMethods ;
2
12
use pyo3_async_runtimes:: tokio:: future_into_py;
3
13
use std:: os:: fd:: FromRawFd ;
4
14
@@ -11,7 +21,13 @@ fn gxhash<T>(hasher: fn(&[u8], i64) -> T, bytes: &[u8], seed: i64) -> PyResult<T
11
21
}
12
22
13
23
fn gxhash_file < T > ( hasher : fn ( & [ u8 ] , i64 ) -> T , file_descriptor : i32 , seed : i64 ) -> PyResult < T > {
14
- let mmap = unsafe { memmap2:: Mmap :: map ( & std:: fs:: File :: from_raw_fd ( libc:: dup ( file_descriptor) ) ) ? } ;
24
+ let duplicated_file_descriptor = unsafe { libc:: dup ( file_descriptor) } ;
25
+
26
+ if duplicated_file_descriptor == -1 {
27
+ return Err ( PyErr :: new :: < pyo3:: exceptions:: PyOSError , _ > ( "Failed to duplicate file descriptor" ) ) ;
28
+ }
29
+
30
+ let mmap = unsafe { memmap2:: Mmap :: map ( & std:: fs:: File :: from_raw_fd ( duplicated_file_descriptor) ) ? } ;
15
31
Ok ( hasher ( & mmap, seed) )
16
32
}
17
33
@@ -47,12 +63,11 @@ impl GxHash32 {
47
63
gxhash ( self . hasher , bytes, self . seed )
48
64
}
49
65
50
- fn hash_async < ' a > ( & self , py : Python < ' a > , bytes : & ' a [ u8 ] ) -> PyResult < Bound < ' a , PyAny > > {
66
+ fn hash_async < ' a > ( & self , py : Python < ' a > , bytes : Py < PyBytes > ) -> PyResult < Bound < ' a , PyAny > > {
51
67
let seed = self . seed ;
52
68
let hasher = self . hasher ;
53
- let bytes_static = unsafe { std:: mem:: transmute :: < & ' a [ u8 ] , & ' static [ u8 ] > ( bytes) } ;
54
69
55
- future_into_py ( py, async move { gxhash ( hasher, bytes_static , seed) } )
70
+ future_into_py ( py, async move { gxhash ( hasher, Python :: with_gil ( |py| bytes . as_bytes ( py ) ) , seed) } )
56
71
}
57
72
58
73
fn hash_file ( & self , py : Python , file : PyObject ) -> PyResult < u32 > {
@@ -82,12 +97,11 @@ impl GxHash64 {
82
97
gxhash ( self . hasher , bytes, self . seed )
83
98
}
84
99
85
- fn hash_async < ' a > ( & self , py : Python < ' a > , bytes : & ' a [ u8 ] ) -> PyResult < Bound < ' a , PyAny > > {
100
+ fn hash_async < ' a > ( & self , py : Python < ' a > , bytes : Py < PyBytes > ) -> PyResult < Bound < ' a , PyAny > > {
86
101
let seed = self . seed ;
87
102
let hasher = self . hasher ;
88
- let bytes_static = unsafe { std:: mem:: transmute :: < & ' a [ u8 ] , & ' static [ u8 ] > ( bytes) } ;
89
103
90
- future_into_py ( py, async move { gxhash ( hasher, bytes_static , seed) } )
104
+ future_into_py ( py, async move { gxhash ( hasher, Python :: with_gil ( |py| bytes . as_bytes ( py ) ) , seed) } )
91
105
}
92
106
93
107
fn hash_file ( & self , py : Python , file : PyObject ) -> PyResult < u64 > {
@@ -117,12 +131,11 @@ impl GxHash128 {
117
131
gxhash ( self . hasher , bytes, self . seed )
118
132
}
119
133
120
- fn hash_async < ' a > ( & self , py : Python < ' a > , bytes : & ' a [ u8 ] ) -> PyResult < Bound < ' a , PyAny > > {
134
+ fn hash_async < ' a > ( & self , py : Python < ' a > , bytes : Py < PyBytes > ) -> PyResult < Bound < ' a , PyAny > > {
121
135
let seed = self . seed ;
122
136
let hasher = self . hasher ;
123
- let bytes_static = unsafe { std:: mem:: transmute :: < & ' a [ u8 ] , & ' static [ u8 ] > ( bytes) } ;
124
137
125
- future_into_py ( py, async move { gxhash ( hasher, bytes_static , seed) } )
138
+ future_into_py ( py, async move { gxhash ( hasher, Python :: with_gil ( |py| bytes . as_bytes ( py ) ) , seed) } )
126
139
}
127
140
128
141
fn hash_file ( & self , py : Python , file : PyObject ) -> PyResult < u128 > {
@@ -138,8 +151,8 @@ impl GxHash128 {
138
151
}
139
152
}
140
153
141
- #[ pymodule( name = "gxhash" ) ]
142
- fn pygxhash ( m : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
154
+ #[ pyo3 :: prelude :: pymodule( name = "gxhash" ) ]
155
+ fn pygxhash ( m : & Bound < ' _ , pyo3 :: prelude :: PyModule > ) -> PyResult < ( ) > {
143
156
m. add_class :: < GxHash32 > ( ) ?;
144
157
m. add_class :: < GxHash64 > ( ) ?;
145
158
m. add_class :: < GxHash128 > ( ) ?;
0 commit comments