Vec<u8>: support u8-compatible buffer exporters#5774
Vec<u8>: support u8-compatible buffer exporters#5774espressolee wants to merge 1 commit intoPyO3:mainfrom
Conversation
CodSpeed Performance ReportMerging this PR will degrade performance by 11.58%Comparing Summary
Performance Changes
Footnotes
|
|
I am unsure I am comfortable with having this be built-in to the Hopefully the Python C API will one day support better guarantees for buffer objects. |
Summary
This PR extends
Vec<u8>extraction to support u8-compatible buffer-protocol exporters (e.g.memoryview,array('B'), and custom types implementing__getbuffer__) by performing a contiguous copy viaPyBuffer<u8>.When the object exports a buffer that is not compatible with
u8(e.g.array('I')), extraction falls back to the existing sequence semantics, preserving current behavior.Why
Vec<u8>is a common "byte payload" type. Today:bytes/bytearrayalready have a specialized path viau8::sequence_extractor.Vec<u8>at all.This change treats the buffer protocol as a first-class source for
Vec<u8>when it is semantically valid (u8-compatible).Implementation notes
u8::sequence_extractorspecialization to recognize buffer exporters.FromPyObjectSequence::to_vecfallible (PyResult<Vec<_>>) so buffer copies can propagate errors cleanly.PyBuffer<u8>implementation to perform the copy.Compatibility
str -> Vec<_>rejection.bytes/bytearraybehavior unchanged.array('I')), behavior is preserved via fallback to sequence semantics.Tests
Added to
tests/test_buffer_protocol.rs:test_extract_vec_u8_from_buffer_exporter(custom buffer exporter, not a sequence)test_extract_vec_u8_falls_back_when_buffer_incompatible(array('I')fallback)