-
Notifications
You must be signed in to change notification settings - Fork 805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for SmallVec
in conversion traits (#3440)
#3507
Merged
davidhewitt
merged 3 commits into
PyO3:main
from
orhun:feat/support_smallvec_conversion
Oct 15, 2023
+147
−0
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add `smallvec` feature to add `ToPyObject`, `IntoPy` and `FromPyObject` implementations for `smallvec::SmallVec`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ pub mod num_bigint; | |
pub mod num_complex; | ||
pub mod rust_decimal; | ||
pub mod serde; | ||
pub mod smallvec; | ||
mod std; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
#![cfg(feature = "smallvec")] | ||
|
||
//! Conversions to and from [smallvec](https://docs.rs/smallvec/). | ||
//! | ||
//! # Setup | ||
//! | ||
//! To use this feature, add this to your **`Cargo.toml`**: | ||
//! | ||
//! ```toml | ||
//! [dependencies] | ||
//! # change * to the latest versions | ||
//! smallvec = "*" | ||
#![doc = concat!("pyo3 = { version = \"", env!("CARGO_PKG_VERSION"), "\", features = [\"smallvec\"] }")] | ||
//! ``` | ||
//! | ||
//! Note that you must use compatible versions of smallvec and PyO3. | ||
//! The required smallvec version may vary based on the version of PyO3. | ||
use crate::exceptions::PyTypeError; | ||
#[cfg(feature = "experimental-inspect")] | ||
use crate::inspect::types::TypeInfo; | ||
use crate::types::list::new_from_iter; | ||
use crate::types::{PySequence, PyString}; | ||
use crate::{ | ||
ffi, FromPyObject, IntoPy, PyAny, PyDowncastError, PyObject, PyResult, Python, ToPyObject, | ||
}; | ||
use smallvec::{Array, SmallVec}; | ||
|
||
impl<A> ToPyObject for SmallVec<A> | ||
where | ||
A: Array, | ||
A::Item: ToPyObject, | ||
{ | ||
fn to_object(&self, py: Python<'_>) -> PyObject { | ||
self.as_slice().to_object(py) | ||
} | ||
} | ||
|
||
impl<A> IntoPy<PyObject> for SmallVec<A> | ||
where | ||
A: Array, | ||
A::Item: IntoPy<PyObject>, | ||
{ | ||
fn into_py(self, py: Python<'_>) -> PyObject { | ||
let mut iter = self.into_iter().map(|e| e.into_py(py)); | ||
let list = new_from_iter(py, &mut iter); | ||
list.into() | ||
} | ||
|
||
#[cfg(feature = "experimental-inspect")] | ||
fn type_output() -> TypeInfo { | ||
TypeInfo::list_of(A::Item::type_output()) | ||
} | ||
} | ||
|
||
impl<'a, A> FromPyObject<'a> for SmallVec<A> | ||
where | ||
A: Array, | ||
A::Item: FromPyObject<'a>, | ||
{ | ||
fn extract(obj: &'a PyAny) -> PyResult<Self> { | ||
if obj.is_instance_of::<PyString>() { | ||
return Err(PyTypeError::new_err("Can't extract `str` to `SmallVec`")); | ||
} | ||
extract_sequence(obj) | ||
} | ||
|
||
#[cfg(feature = "experimental-inspect")] | ||
fn type_input() -> TypeInfo { | ||
TypeInfo::sequence_of(A::Item::type_input()) | ||
} | ||
} | ||
|
||
fn extract_sequence<'s, A>(obj: &'s PyAny) -> PyResult<SmallVec<A>> | ||
where | ||
A: Array, | ||
A::Item: FromPyObject<'s>, | ||
{ | ||
// Types that pass `PySequence_Check` usually implement enough of the sequence protocol | ||
// to support this function and if not, we will only fail extraction safely. | ||
let seq: &PySequence = unsafe { | ||
if ffi::PySequence_Check(obj.as_ptr()) != 0 { | ||
obj.downcast_unchecked() | ||
} else { | ||
return Err(PyDowncastError::new(obj, "Sequence").into()); | ||
} | ||
}; | ||
|
||
let mut sv = SmallVec::with_capacity(seq.len().unwrap_or(0)); | ||
for item in seq.iter()? { | ||
sv.push(item?.extract::<A::Item>()?); | ||
} | ||
Ok(sv) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::types::{PyDict, PyList}; | ||
|
||
#[test] | ||
fn test_smallvec_into_py() { | ||
Python::with_gil(|py| { | ||
let sv: SmallVec<[u64; 8]> = [1, 2, 3, 4, 5].iter().cloned().collect(); | ||
let hso: PyObject = sv.clone().into_py(py); | ||
let l = PyList::new(py, [1, 2, 3, 4, 5]); | ||
assert!(l.eq(hso).unwrap()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_smallvec_from_py_object() { | ||
Python::with_gil(|py| { | ||
let l = PyList::new(py, [1, 2, 3, 4, 5]); | ||
let sv: SmallVec<[u64; 8]> = l.extract().unwrap(); | ||
assert_eq!(sv.as_slice(), [1, 2, 3, 4, 5]); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_smallvec_from_py_object_fails() { | ||
Python::with_gil(|py| { | ||
let dict = PyDict::new(py); | ||
let sv: PyResult<SmallVec<[u64; 8]>> = dict.extract(); | ||
assert_eq!( | ||
sv.unwrap_err().to_string(), | ||
"TypeError: 'dict' object cannot be converted to 'Sequence'" | ||
); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_smallvec_to_object() { | ||
Python::with_gil(|py| { | ||
let sv: SmallVec<[u64; 8]> = [1, 2, 3, 4, 5].iter().cloned().collect(); | ||
let hso: PyObject = sv.to_object(py); | ||
let l = PyList::new(py, [1, 2, 3, 4, 5]); | ||
assert!(l.eq(hso).unwrap()); | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this not use version 1.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean?
"smallvec = 1.0"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in general we try to accept as broad ranges of optional dependency versions as possible so that we don't have to force users to use specific versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, made the change in 87cbd38