Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
proximity
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mathis "what could possibly go wrong" Randl
proximity
Commits
d34ab6a1
Commit
d34ab6a1
authored
2 months ago
by
Mathis "what could possibly go wrong" Randl
Browse files
Options
Downloads
Patches
Plain Diff
doubly generic cache + vector handling
parent
8717dc7d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bindings/src/api.rs
+48
-6
48 additions, 6 deletions
bindings/src/api.rs
bindings/src/lib.rs
+2
-2
2 additions, 2 deletions
bindings/src/lib.rs
with
50 additions
and
8 deletions
bindings/src/api.rs
+
48
−
6
View file @
d34ab6a1
use
proximipy
::
caching
::
approximate_cache
::
ApproximateCache
;
use
proximipy
::
caching
::
bounded
::
bounded_linear_cache
::
BoundedLinearCache
;
use
pyo3
::{
pyclass
,
pymethods
};
use
pyo3
::{
pyclass
,
pymethods
,
types
::{
PyAnyMethods
,
PyList
},
Bound
,
FromPyObject
,
IntoPyObject
,
PyErr
};
macro_rules!
create_pythonized_interface
{
(
$name
:
ident
,
$type
:
ident
)
=>
{
(
$name
:
ident
,
$
key
type
:
ident
,
$valuetype
:
ident
)
=>
{
// unsendable == should hard-crash if Python tries to access it from
// two different Python threads.
//
...
...
@@ -15,7 +15,7 @@ macro_rules! create_pythonized_interface {
// happen on the Rust side and will not be visible to the Python ML pipeline.
#[pyclass(unsendable)]
pub
struct
$name
{
inner
:
BoundedLinearCache
<
$type
,
$type
>
,
inner
:
BoundedLinearCache
<
$
key
type
,
$
value
type
>
,
}
#[pymethods]
...
...
@@ -27,19 +27,61 @@ macro_rules! create_pythonized_interface {
}
}
fn
find
(
&
mut
self
,
k
:
$type
)
->
Option
<
$type
>
{
fn
find
(
&
mut
self
,
k
:
$
key
type
)
->
Option
<
$
value
type
>
{
self
.inner
.find
(
&
k
)
}
fn
insert
(
&
mut
self
,
key
:
$type
,
value
:
$type
)
{
fn
insert
(
&
mut
self
,
key
:
$
key
type
,
value
:
$
value
type
)
{
self
.inner
.insert
(
key
,
value
)
}
fn
len
(
&
self
)
->
usize
{
self
.inner
.len
()
}
fn
__len__
(
&
self
)
->
usize
{
self
.len
()
}
}
};
}
struct
F32VecPy
{
inner
:
Vec
<
f32
>
}
/// Explain to Rust how to parse some random python object into an actual Rust vector
/// This involves new allocations because Python cannot be trusted to keep this
/// reference alive.
///
/// This can fail if the random object in question is not a list of numbers,
/// in which case it is automatically reported by raising a TypeError exception
/// in the Python code
impl
<
'a
>
FromPyObject
<
'a
>
for
F32VecPy
{
fn
extract_bound
(
ob
:
&
pyo3
::
Bound
<
'a
,
pyo3
::
PyAny
>
)
->
pyo3
::
PyResult
<
Self
>
{
let
list
:
Vec
<
f32
>
=
ob
.downcast
::
<
PyList
>
()
?
.extract
()
?
;
Ok
(
F32VecPy
{
inner
:
list
})
}
}
// Cast back the list of floats to a Python list
impl
<
'a
>
IntoPyObject
<
'a
>
for
F32VecPy
{
type
Target
=
PyList
;
type
Output
=
Bound
<
'a
,
PyList
>
;
type
Error
=
PyErr
;
fn
into_pyobject
(
self
,
py
:
pyo3
::
Python
<
'a
>
)
->
Result
<
Self
::
Output
,
Self
::
Error
>
{
let
internal
=
self
.inner
;
PyList
::
new
(
py
,
internal
)
}
}
impl
Clone
for
F32VecPy
{
fn
clone
(
&
self
)
->
Self
{
F32VecPy
{
inner
:
self
.inner
.clone
()
}
}
}
create_pythonized_interface!
(
I16Cache
,
i16
);
create_pythonized_interface!
(
I16
ToVector
Cache
,
i16
,
F32VecPy
);
\ No newline at end of file
This diff is collapsed.
Click to expand it.
bindings/src/lib.rs
+
2
−
2
View file @
d34ab6a1
use
api
::
I16Cache
;
use
api
::
I16
ToVector
Cache
;
use
pyo3
::
prelude
::
*
;
mod
api
;
...
...
@@ -6,6 +6,6 @@ mod api;
/// A Python module implemented in Rust.
#[pymodule]
fn
proximipy
(
m
:
&
Bound
<
'_
,
PyModule
>
)
->
PyResult
<
()
>
{
m
.add_class
::
<
I16Cache
>
()
?
;
m
.add_class
::
<
I16
ToVector
Cache
>
()
?
;
Ok
(())
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment