Skip to content

Commit 8075fa0

Browse files
danieldkDaniël de Kok
authored andcommitted
Update to pyo3 0.6
1 parent 028e0c2 commit 8075fa0

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ matrix:
1515
fast_finish: true
1616
include:
1717
- os: linux
18-
rust: nightly-2019-02-04
18+
rust: nightly-2019-02-07
1919
- os: osx
20-
rust: nightly-2019-02-04
20+
rust: nightly-2019-02-07
2121

2222
install:
2323
- |
@@ -33,7 +33,7 @@ install:
3333
pip install cffi virtualenv
3434
fi
3535
- cargo install pyo3-pack
36-
- rustup default nightly-2019-02-04
36+
- rustup default nightly-2019-02-07
3737
- rustup component add rustfmt
3838

3939
script:

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ name = "finalfusion"
99
crate-type = ["cdylib"]
1010

1111
[dependencies.pyo3]
12-
version = "0.5"
12+
version = "0.6"
1313
features = ["extension-module"]
1414

1515
[dependencies]
1616
failure = "0.1"
1717
finalfusion = "0.5"
18+
libc = "0.2"
1819
toml = "0.4"

src/lib.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use toml::{self, Value};
1818
///
1919
/// finalfusion is a format for word embeddings that supports words,
2020
/// subwords, memory-mapped matrices, and quantized matrices.
21-
#[pymodinit]
21+
#[pymodule]
2222
fn finalfusion(_py: Python, m: &PyModule) -> PyResult<()> {
2323
m.add_class::<PyEmbeddings>()?;
2424
m.add_class::<PyWordSimilarity>()?;
@@ -31,13 +31,11 @@ fn finalfusion(_py: Python, m: &PyModule) -> PyResult<()> {
3131
/// vectors) and 1 (identical vectors).
3232
#[pyclass(name=WordSimilarity)]
3333
struct PyWordSimilarity {
34-
#[prop(get)]
34+
#[pyo3(get)]
3535
word: String,
3636

37-
#[prop(get)]
37+
#[pyo3(get)]
3838
similarity: f32,
39-
40-
token: PyToken,
4139
}
4240

4341
#[pyproto]
@@ -69,7 +67,6 @@ struct PyEmbeddings {
6967
// to its method scope.
7068
// 3. None of the methods returns borrowed embeddings.
7169
embeddings: Rc<RefCell<EmbeddingsWrap>>,
72-
token: PyToken,
7370
}
7471

7572
#[pymethods]
@@ -93,7 +90,9 @@ impl PyEmbeddings {
9390
.map_err(|err| exceptions::IOError::py_err(err.to_string()))?,
9491
};
9592

96-
obj.init(|token| PyEmbeddings { embeddings, token })
93+
obj.init(PyEmbeddings { embeddings });
94+
95+
Ok(())
9796
}
9897

9998
/// Perform an anology query.
@@ -128,11 +127,13 @@ impl PyEmbeddings {
128127
let mut r = Vec::with_capacity(results.len());
129128
for ws in results {
130129
r.push(
131-
Py::new(py, |token| PyWordSimilarity {
132-
word: ws.word.to_owned(),
133-
similarity: ws.similarity.into_inner(),
134-
token,
135-
})?
130+
Py::new(
131+
py,
132+
PyWordSimilarity {
133+
word: ws.word.to_owned(),
134+
similarity: ws.similarity.into_inner(),
135+
},
136+
)?
136137
.into_object(py),
137138
)
138139
}
@@ -226,11 +227,13 @@ impl PyEmbeddings {
226227
let mut r = Vec::with_capacity(results.len());
227228
for ws in results {
228229
r.push(
229-
Py::new(py, |token| PyWordSimilarity {
230-
word: ws.word.to_owned(),
231-
similarity: ws.similarity.into_inner(),
232-
token,
233-
})?
230+
Py::new(
231+
py,
232+
PyWordSimilarity {
233+
word: ws.word.to_owned(),
234+
similarity: ws.similarity.into_inner(),
235+
},
236+
)?
234237
.into_object(py),
235238
)
236239
}
@@ -259,14 +262,16 @@ impl PyEmbeddings {
259262

260263
#[pyproto]
261264
impl PyIterProtocol for PyEmbeddings {
262-
fn __iter__(&mut self) -> PyResult<PyObject> {
265+
fn __iter__(slf: PyRefMut<Self>) -> PyResult<PyObject> {
263266
let gil = Python::acquire_gil();
264267
let py = gil.python();
265-
let iter = Py::new(py, |token| PyEmbeddingIterator {
266-
embeddings: self.embeddings.clone(),
267-
idx: 0,
268-
token,
269-
})?
268+
let iter = Py::new(
269+
py,
270+
PyEmbeddingIterator {
271+
embeddings: slf.embeddings.clone(),
272+
idx: 0,
273+
},
274+
)?
270275
.into_object(py);
271276

272277
Ok(iter)
@@ -293,33 +298,34 @@ where
293298
struct PyEmbeddingIterator {
294299
embeddings: Rc<RefCell<EmbeddingsWrap>>,
295300
idx: usize,
296-
token: PyToken,
297301
}
298302

299303
#[pyproto]
300304
impl PyIterProtocol for PyEmbeddingIterator {
301-
fn __iter__(&mut self) -> PyResult<PyObject> {
302-
Ok(self.into())
305+
fn __iter__(slf: PyRefMut<Self>) -> PyResult<Py<PyEmbeddingIterator>> {
306+
Ok(slf.into())
303307
}
304308

305-
fn __next__(&mut self) -> PyResult<Option<(String, Vec<f32>)>> {
306-
let embeddings = self.embeddings.borrow();
309+
fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<(String, Vec<f32>)>> {
310+
let slf = &mut *slf;
311+
312+
let embeddings = slf.embeddings.borrow();
307313

308314
use EmbeddingsWrap::*;
309315
let vocab = match &*embeddings {
310316
View(e) => e.vocab(),
311317
NonView(e) => e.vocab(),
312318
};
313319

314-
if self.idx < vocab.len() {
315-
let word = vocab.words()[self.idx].to_string();
320+
if slf.idx < vocab.len() {
321+
let word = vocab.words()[slf.idx].to_string();
316322

317323
let embed = match &*embeddings {
318-
View(e) => e.storage().embedding(self.idx),
319-
NonView(e) => e.storage().embedding(self.idx),
324+
View(e) => e.storage().embedding(slf.idx),
325+
NonView(e) => e.storage().embedding(slf.idx),
320326
};
321327

322-
self.idx += 1;
328+
slf.idx += 1;
323329

324330
Ok(Some((word, embed.as_view().to_vec())))
325331
} else {

0 commit comments

Comments
 (0)