| 1 | //! Support for Python buffer protocol |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::rawrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/::c_intrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/c_int#; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#; |
| 6 | |
| 7 | use bytesrust-analyzer cargo bytes 1.10.1 crate/::{Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes#, BytesMutrust-analyzer cargo bytes 1.10.1 bytes_mut/BytesMut#}; |
| 8 | use pyo3rust-analyzer cargo pyo3 0.28.0 crate/::bufferrust-analyzer cargo pyo3 0.28.0 buffer/::PyBufferrust-analyzer cargo pyo3 0.28.0 buffer/PyBuffer#; |
| 9 | use pyo3rust-analyzer cargo pyo3 0.28.0 crate/::exceptionsrust-analyzer cargo pyo3 0.28.0 exceptions/::{PyIndexErrorrust-analyzer cargo pyo3 0.28.0 exceptions/PyIndexError#, PyValueErrorrust-analyzer cargo pyo3 0.28.0 exceptions/PyValueError#}; |
| 10 | use pyo3rust-analyzer cargo pyo3 0.28.0 crate/::preluderust-analyzer cargo pyo3 0.28.0 prelude/::*; |
| 11 | use pyo3rust-analyzer cargo pyo3 0.28.0 crate/::typesrust-analyzer cargo pyo3 0.28.0 types/::{PyDictrust-analyzer cargo pyo3 0.28.0 types/dict/PyDict#, PySlicerust-analyzer cargo pyo3 0.28.0 types/slice/PySlice#, PyTuplerust-analyzer cargo pyo3 0.28.0 types/tuple/PyTuple#}; |
| 12 | use pyo3rust-analyzer cargo pyo3 0.28.0 crate/::{ffirust-analyzer cargo pyo3 0.28.0 ffi/, IntoPyObjectExtrust-analyzer cargo pyo3 0.28.0 conversion/IntoPyObjectExt#}; |
| 13 | |
| 14 | /// A wrapper around a [`bytes::Bytes`][]. |
| 15 | /// |
| 16 | /// This implements both import and export via the Python buffer protocol. |
| 17 | /// |
| 18 | /// ### Buffer protocol import |
| 19 | /// |
| 20 | /// This can be very useful as a general way to support ingest of a Python buffer protocol object. |
| 21 | /// |
| 22 | /// The underlying [Bytes] manages the external memory, automatically calling the Python |
| 23 | /// buffer's release callback when the internal reference count reaches 0. |
| 24 | /// |
| 25 | /// Note that converting this [`Bytes`] into a [BytesMut][::bytes::BytesMut] will always create a |
| 26 | /// deep copy of the buffer into newly allocated memory, since this `Bytes` is constructed from an |
| 27 | /// owner. |
| 28 | /// |
| 29 | /// ### Buffer protocol export |
| 30 | /// |
| 31 | /// PyBytes implements the Python buffer protocol to enable Python to access the underlying `Bytes` |
| 32 | /// data view without copies. In Python, this `PyBytes` object can be passed to Python `bytes` or |
| 33 | /// `memoryview` constructors, `numpy.frombuffer`, or any other function that supports buffer |
| 34 | /// protocol input. |
| 35 | #[pyclassrust-analyzer cargo pyo3-macros 0.28.0 pyclass:(name = "Bytes", subclass, frozen, sequence, weakref)] |
| 36 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 37 | pub struct PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes#); |
| 38 | |
| 39 | impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes#> for PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 40 | fn as_refrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`AsRef<Bytes>`]as_ref().(&self) -> &Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes# { |
| 41 | &self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0. |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> for PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 46 | fn as_refrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`AsRef<[u8]>`]as_ref().(&self) -> &[u8] { |
| 47 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 52 | /// Construct a new [PyBytes] |
| 53 | pub fn newrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]new().(buffer: Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes#) -> Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes] { |
| 54 | Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes](buffer) |
| 55 | } |
| 56 | |
| 57 | /// Consume and return the [Bytes] |
| 58 | pub fn into_innerrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]into_inner().(self) -> Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes# { |
| 59 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0. |
| 60 | } |
| 61 | |
| 62 | /// Access the underlying buffer as a byte slice |
| 63 | pub fn as_slicerust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]as_slice().(&self) -> &[u8] { |
| 64 | self.as_refrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`AsRef<[u8]>`]as_ref().() |
| 65 | } |
| 66 | |
| 67 | /// Slice the underlying buffer using a Python slice object |
| 68 | /// |
| 69 | /// This should behave the same as Python's byte slicing: |
| 70 | /// - `ValueError` if step is zero |
| 71 | /// - Negative indices a-ok |
| 72 | /// - If start/stop are out of bounds, they are clipped to the bounds of the buffer |
| 73 | /// - If start > stop, the slice is empty |
| 74 | /// |
| 75 | /// This is NOT exposed to Python under the `#[pymethods]` impl |
| 76 | fn slicerust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]slice().(&self, slice: &Boundrust-analyzer cargo pyo3 0.28.0 instance/Bound#<'_, PySlicerust-analyzer cargo pyo3 0.28.0 types/slice/PySlice#>) -> PyResultrust-analyzer cargo pyo3 0.28.0 err/PyResult#<PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#> { |
| 77 | let bytes_length = self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().() as isize; |
| 78 | let (start, stop, step) = { |
| 79 | let slice_indices = slice.indicesrust-analyzer cargo pyo3 0.28.0 types/slice/impl#[`Bound<'py, PySlice>`][`PySliceMethods<'py>`]indices().(bytes_length)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 80 | (slice_indices.startrust-analyzer cargo pyo3 0.28.0 types/slice/PySliceIndices#start., slice_indices.stoprust-analyzer cargo pyo3 0.28.0 types/slice/PySliceIndices#stop., slice_indices.steprust-analyzer cargo pyo3 0.28.0 types/slice/PySliceIndices#step.) |
| 81 | }; |
| 82 | |
| 83 | let new_capacity = if (step rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt().0 && stop rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt().start) || (step rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().0 && stop rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().start) { |
| 84 | (((stop rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub().start).absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]abs().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Add<Self>`]add().step.absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]abs().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub().1) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Div<Self>`]div().step.absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]abs().()) as usize |
| 85 | } else { |
| 86 | 0 |
| 87 | }; |
| 88 | |
| 89 | if new_capacity rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 90 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]new().())); |
| 91 | } |
| 92 | if step rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]eq().1 { |
| 93 | // if start < 0 and stop > len and step == 1 just copy? |
| 94 | if start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().0 && stop rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]ge().bytes_length { |
| 95 | let out = self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..slicerust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]slice().(..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#); |
| 96 | let py_bytes = PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(out); |
| 97 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(py_bytes); |
| 98 | } |
| 99 | |
| 100 | if start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]ge().0 && stop rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]le().bytes_length && start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt().stop { |
| 101 | let out = self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..slicerust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]slice().(start as usize..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#stop as usize); |
| 102 | let py_bytes = PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(out); |
| 103 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(py_bytes); |
| 104 | } |
| 105 | // fall through to the general case here... |
| 106 | } |
| 107 | if step rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]gt().0 { |
| 108 | // forward |
| 109 | let mut new_buf = BytesMutrust-analyzer cargo bytes 1.10.1 bytes_mut/BytesMut#::with_capacityrust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut]with_capacity().(new_capacity); |
| 110 | new_buf.extendrust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut][`Extend<u8>`]extend().( |
| 111 | (start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#stop) |
| 112 | .step_byrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#step_by().(step as usize) |
| 113 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|i| self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().), |
| 114 | ); |
| 115 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(new_buf.freezerust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut]freeze().())) |
| 116 | } else { |
| 117 | // backward |
| 118 | let mut new_buf = BytesMutrust-analyzer cargo bytes 1.10.1 bytes_mut/BytesMut#::with_capacityrust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut]with_capacity().(new_capacity); |
| 119 | new_buf.extendrust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut][`Extend<u8>`]extend().( |
| 120 | (stop rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Add<Self>`]add().1..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#start) |
| 121 | .revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().() |
| 122 | .step_byrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#step_by().((-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][Neg]neg().step) as usize) |
| 123 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|i| self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().), |
| 124 | ); |
| 125 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(new_buf.freezerust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut]freeze().())) |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#> for Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes# { |
| 131 | fn fromrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[Bytes][`From<PyBytes>`]from().(value: PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#) -> Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[Bytes][`From<PyBytes>`] { |
| 132 | value.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0. |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> for PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 137 | fn fromrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`From<Vec<u8>>`]from().(value: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`From<Vec<u8>>`] { |
| 138 | PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes#> for PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 143 | fn fromrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`From<Bytes>`]from().(value: Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes#) -> Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`From<Bytes>`] { |
| 144 | PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(value) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<BytesMutrust-analyzer cargo bytes 1.10.1 bytes_mut/BytesMut#> for PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 149 | fn fromrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`From<BytesMut>`]from().(value: BytesMutrust-analyzer cargo bytes 1.10.1 bytes_mut/BytesMut#) -> Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`From<BytesMut>`] { |
| 150 | PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #[pymethodsrust-analyzer cargo pyo3-macros 0.28.0 pymethods:] |
| 155 | impl PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 156 | // By setting the argument to PyBytes, this means that any buffer-protocol object is supported |
| 157 | // here, since it will use the FromPyObject impl. |
| 158 | #[new] |
| 159 | #[pyo3(signature = (buf = PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#(Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]new().())), text_signature = "(buf = b'')")] |
| 160 | fn py_newrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]py_new().(buf: PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#) -> Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes] { |
| 161 | buf |
| 162 | } |
| 163 | |
| 164 | fn __getnewargs_ex__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__getnewargs_ex__().<'py>(&'py self, py: Pythonrust-analyzer cargo pyo3 0.28.0 marker/Python#<'py>) -> PyResultrust-analyzer cargo pyo3 0.28.0 err/PyResult#<Boundrust-analyzer cargo pyo3 0.28.0 instance/Bound#<'py, PyTuplerust-analyzer cargo pyo3 0.28.0 types/tuple/PyTuple#>> { |
| 165 | let py_bytes = self.to_bytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]to_bytes().(py); |
| 166 | let args = PyTuplerust-analyzer cargo pyo3 0.28.0 types/tuple/PyTuple#::newrust-analyzer cargo pyo3 0.28.0 types/tuple/impl#[PyTuple]new().(py, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![py_bytes])?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..into_bound_py_anyrust-analyzer cargo pyo3 0.28.0 conversion/IntoPyObjectExt#into_bound_py_any().(py)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 167 | let kwargs = PyDictrust-analyzer cargo pyo3 0.28.0 types/dict/PyDict#::newrust-analyzer cargo pyo3 0.28.0 types/dict/impl#[PyDict]new().(py).into_bound_py_anyrust-analyzer cargo pyo3 0.28.0 conversion/IntoPyObjectExt#into_bound_py_any().(py)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 168 | PyTuplerust-analyzer cargo pyo3 0.28.0 types/tuple/PyTuple#::newrust-analyzer cargo pyo3 0.28.0 types/tuple/impl#[PyTuple]new().(py, [args, kwargs]) |
| 169 | } |
| 170 | |
| 171 | /// The number of bytes in this Bytes |
| 172 | fn __len__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__len__().(&self) -> usize { |
| 173 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().() |
| 174 | } |
| 175 | |
| 176 | fn __repr__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__repr__().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 177 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{self:?}") |
| 178 | } |
| 179 | |
| 180 | fn __add__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__add__().(&self, other: PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#) -> PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 181 | let total_length = self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().() +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). other.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().(); |
| 182 | let mut new_buffer = BytesMutrust-analyzer cargo bytes 1.10.1 bytes_mut/BytesMut#::with_capacityrust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut]with_capacity().(total_length); |
| 183 | new_buffer.extend_from_slicerust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut]extend_from_slice().(&self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0.); |
| 184 | new_buffer.extend_from_slicerust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut]extend_from_slice().(&other.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0.); |
| 185 | new_buffer.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 186 | } |
| 187 | |
| 188 | fn __contains__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__contains__().(&self, item: PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#) -> bool { |
| 189 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0. |
| 190 | .windowsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]windows().(item.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().()) |
| 191 | .anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#any().(|window| window ==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). item.as_slicerust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]as_slice().()) |
| 192 | } |
| 193 | |
| 194 | fn __eq__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__eq__().(&self, other: PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#) -> bool { |
| 195 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() ==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). other.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() |
| 196 | } |
| 197 | |
| 198 | fn __getitem__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__getitem__().<'py>( |
| 199 | &self, |
| 200 | py: Pythonrust-analyzer cargo pyo3 0.28.0 marker/Python#<'py>, |
| 201 | key: BytesGetItemKeyrust-analyzer cargo pyo3-bytes 0.6.0 bytes/BytesGetItemKey#<'py>, |
| 202 | ) -> PyResultrust-analyzer cargo pyo3 0.28.0 err/PyResult#<Boundrust-analyzer cargo pyo3 0.28.0 instance/Bound#<'py, PyAnyrust-analyzer cargo pyo3 0.28.0 types/any/PyAny#>> { |
| 203 | match key { |
| 204 | BytesGetItemKeyrust-analyzer cargo pyo3-bytes 0.6.0 bytes/BytesGetItemKey#::Intrust-analyzer cargo pyo3-bytes 0.6.0 bytes/BytesGetItemKey#Int#(mut index) => { |
| 205 | if index <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt(). 0 { |
| 206 | index +=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign(). self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().() as isize; |
| 207 | } |
| 208 | if index <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]lt(). 0 { |
| 209 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PyIndexErrorrust-analyzer cargo pyo3 0.28.0 exceptions/PyIndexError#::new_errrust-analyzer cargo pyo3 0.28.0 exceptions/impl#[PyIndexError]new_err().("Index out of range")); |
| 210 | } |
| 211 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0. |
| 212 | .getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(index as usize) |
| 213 | .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(PyIndexErrorrust-analyzer cargo pyo3 0.28.0 exceptions/PyIndexError#::new_errrust-analyzer cargo pyo3 0.28.0 exceptions/impl#[PyIndexError]new_err().("Index out of range"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 214 | .into_bound_py_anyrust-analyzer cargo pyo3 0.28.0 conversion/IntoPyObjectExt#into_bound_py_any().(py) |
| 215 | } |
| 216 | BytesGetItemKeyrust-analyzer cargo pyo3-bytes 0.6.0 bytes/BytesGetItemKey#::Slicerust-analyzer cargo pyo3-bytes 0.6.0 bytes/BytesGetItemKey#Slice#(slice) => { |
| 217 | let s = self.slicerust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]slice().(&slice)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 218 | s.into_bound_py_anyrust-analyzer cargo pyo3 0.28.0 conversion/IntoPyObjectExt#into_bound_py_any().(py) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | fn __mul__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__mul__().(&self, value: usize) -> PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 224 | let mut out_buf = BytesMutrust-analyzer cargo bytes 1.10.1 bytes_mut/BytesMut#::with_capacityrust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut]with_capacity().(self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().() *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). value); |
| 225 | (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#value).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|_| out_buf.extend_from_slicerust-analyzer cargo bytes 1.10.1 bytes_mut/impl#[BytesMut]extend_from_slice().(self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().())); |
| 226 | out_buf.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 227 | } |
| 228 | |
| 229 | /// This is taken from opendal: |
| 230 | /// https://github.com/apache/opendal/blob/d001321b0f9834bc1e2e7d463bcfdc3683e968c9/bindings/python/src/utils.rs#L51-L72 |
| 231 | #[allow(unsafe_code)] |
| 232 | unsafe fn __getbuffer__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__getbuffer__().( |
| 233 | slf: PyRefrust-analyzer cargo pyo3 0.28.0 pycell/PyRef#<Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]>, |
| 234 | view: *mut ffirust-analyzer cargo pyo3 0.28.0 ffi/::Py_bufferrust-analyzer cargo pyo3-ffi 0.28.0 pybuffer/Py_buffer#, |
| 235 | flags: c_intrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/raw/c_int#, |
| 236 | ) -> PyResultrust-analyzer cargo pyo3 0.28.0 err/PyResult#<()> { |
| 237 | let bytes = slf.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().(); |
| 238 | let ret = ffirust-analyzer cargo pyo3 0.28.0 ffi/::PyBuffer_FillInforust-analyzer cargo pyo3-ffi 0.28.0 PyBuffer_FillInfo().( |
| 239 | view, |
| 240 | slf.as_ptrrust-analyzer cargo pyo3 0.28.0 pycell/impl#[`PyRef<'py, T>`]as_ptr().() as *mut _, |
| 241 | bytes.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().() as *mut _, |
| 242 | bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().().try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 243 | 1, // read only |
| 244 | flags, |
| 245 | ); |
| 246 | if ret ==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1 { |
| 247 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PyErrrust-analyzer cargo pyo3 0.28.0 err/PyErr#::fetchrust-analyzer cargo pyo3 0.28.0 err/impl#[PyErr]fetch().(slf.pyrust-analyzer cargo pyo3 0.28.0 pycell/impl#[`PyRef<'p, T>`]py().())); |
| 248 | } |
| 249 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 250 | } |
| 251 | |
| 252 | // Comment from david hewitt on discord: |
| 253 | // > I think normally `__getbuffer__` takes a pointer to the owning Python object, so you |
| 254 | // > don't need to treat the allocation as owned separately. It should be good enough to keep |
| 255 | // > the allocation owned by the object. |
| 256 | // https://discord.com/channels/1209263839632424990/1324816949464666194/1328299411427557397 |
| 257 | #[allow(unsafe_code)] |
| 258 | unsafe fn __releasebuffer__rust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]__releasebuffer__().(&self, _view: *mut ffirust-analyzer cargo pyo3 0.28.0 ffi/::Py_bufferrust-analyzer cargo pyo3-ffi 0.28.0 pybuffer/Py_buffer#) {} |
| 259 | |
| 260 | /// If the binary data starts with the prefix string, return bytes[len(prefix):]. Otherwise, |
| 261 | /// return a copy of the original binary data: |
| 262 | #[pyo3(signature = (prefix, /))] |
| 263 | fn removeprefixrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]removeprefix().(&self, prefix: PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#) -> PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 264 | if self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]starts_with().(prefix.as_refrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`AsRef<[u8]>`]as_ref().()) { |
| 265 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..slicerust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]slice().(prefix.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().()..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 266 | } else { |
| 267 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..clonerust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][Clone]clone().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /// If the binary data ends with the suffix string and that suffix is not empty, return |
| 272 | /// `bytes[:-len(suffix)]`. Otherwise, return the original binary data. |
| 273 | #[pyo3(signature = (suffix, /))] |
| 274 | fn removesuffixrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]removesuffix().(&self, suffix: PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#) -> PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 275 | if self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]ends_with().(suffix.as_refrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`AsRef<[u8]>`]as_ref().()) { |
| 276 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..slicerust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]slice().(0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). suffix.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..lenrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]len().()).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 277 | } else { |
| 278 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..clonerust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][Clone]clone().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /// Return True if all bytes in the sequence are alphabetical ASCII characters or ASCII decimal |
| 283 | /// digits and the sequence is not empty, False otherwise. Alphabetic ASCII characters are |
| 284 | /// those byte values in the sequence b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'. |
| 285 | /// ASCII decimal digits are those byte values in the sequence b'0123456789'. |
| 286 | fn isalnumrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]isalnum().(&self) -> bool { |
| 287 | if self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..is_emptyrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]is_empty().() { |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | for c in self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() { |
| 292 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_ascii_alphanumericrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_alphanumeric().() { |
| 293 | return false; |
| 294 | } |
| 295 | } |
| 296 | true |
| 297 | } |
| 298 | |
| 299 | /// Return True if all bytes in the sequence are alphabetic ASCII characters and the sequence |
| 300 | /// is not empty, False otherwise. Alphabetic ASCII characters are those byte values in the |
| 301 | /// sequence b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'. |
| 302 | fn isalpharust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]isalpha().(&self) -> bool { |
| 303 | if self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..is_emptyrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]is_empty().() { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | for c in self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() { |
| 308 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_ascii_alphabeticrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_alphabetic().() { |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | true |
| 313 | } |
| 314 | |
| 315 | /// Return True if the sequence is empty or all bytes in the sequence are ASCII, False |
| 316 | /// otherwise. ASCII bytes are in the range 0-0x7F. |
| 317 | fn isasciirust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]isascii().(&self) -> bool { |
| 318 | for c in self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() { |
| 319 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_asciirust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii().() { |
| 320 | return false; |
| 321 | } |
| 322 | } |
| 323 | true |
| 324 | } |
| 325 | |
| 326 | /// Return True if all bytes in the sequence are ASCII decimal digits and the sequence is not |
| 327 | /// empty, False otherwise. ASCII decimal digits are those byte values in the sequence |
| 328 | /// b'0123456789'. |
| 329 | fn isdigitrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]isdigit().(&self) -> bool { |
| 330 | if self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..is_emptyrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]is_empty().() { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | for c in self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() { |
| 335 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_digit().() { |
| 336 | return false; |
| 337 | } |
| 338 | } |
| 339 | true |
| 340 | } |
| 341 | |
| 342 | /// Return True if there is at least one lowercase ASCII character in the sequence and no |
| 343 | /// uppercase ASCII characters, False otherwise. |
| 344 | fn islowerrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]islower().(&self) -> bool { |
| 345 | let mut has_lower = false; |
| 346 | for c in self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() { |
| 347 | if c.is_ascii_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_uppercase().() { |
| 348 | return false; |
| 349 | } |
| 350 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().has_lower && c.is_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_lowercase().() { |
| 351 | has_lower = true; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | has_lower |
| 356 | } |
| 357 | |
| 358 | /// Return True if all bytes in the sequence are ASCII whitespace and the sequence is not |
| 359 | /// empty, False otherwise. ASCII whitespace characters are those byte values in the sequence |
| 360 | /// b' \t\n\r\x0b\f' (space, tab, newline, carriage return, vertical tab, form feed). |
| 361 | fn isspacerust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]isspace().(&self) -> bool { |
| 362 | if self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..is_emptyrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]is_empty().() { |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | for c in self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() { |
| 367 | // Also check for vertical tab |
| 368 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().(c.is_ascii_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_whitespace().() || *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().c ==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). b'\x0b') { |
| 369 | return false; |
| 370 | } |
| 371 | } |
| 372 | true |
| 373 | } |
| 374 | |
| 375 | /// Return True if there is at least one uppercase alphabetic ASCII character in the sequence |
| 376 | /// and no lowercase ASCII characters, False otherwise. |
| 377 | fn isupperrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]isupper().(&self) -> bool { |
| 378 | let mut has_upper = false; |
| 379 | for c in self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() { |
| 380 | if c.is_ascii_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_lowercase().() { |
| 381 | return false; |
| 382 | } |
| 383 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().has_upper && c.is_ascii_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_uppercase().() { |
| 384 | has_upper = true; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | has_upper |
| 389 | } |
| 390 | |
| 391 | /// Return a copy of the sequence with all the uppercase ASCII characters converted to their |
| 392 | /// corresponding lowercase counterpart. |
| 393 | fn lowerrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]lower().(&self) -> PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 394 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..to_ascii_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[u8]`]to_ascii_lowercase().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 395 | } |
| 396 | |
| 397 | /// Return a copy of the sequence with all the lowercase ASCII characters converted to their |
| 398 | /// corresponding uppercase counterpart. |
| 399 | fn upperrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]upper().(&self) -> PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 400 | self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..to_ascii_uppercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[u8]`]to_ascii_uppercase().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 401 | } |
| 402 | |
| 403 | /// Copy this buffer's contents to a Python `bytes` object |
| 404 | fn to_bytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes]to_bytes().<'py>(&'py self, py: Pythonrust-analyzer cargo pyo3 0.28.0 marker/Python#<'py>) -> Boundrust-analyzer cargo pyo3 0.28.0 instance/Bound#<'py, pyo3rust-analyzer cargo pyo3 0.28.0 crate/::typesrust-analyzer cargo pyo3 0.28.0 types/::PyBytesrust-analyzer cargo pyo3 0.28.0 types/bytes/PyBytes#> { |
| 405 | pyo3rust-analyzer cargo pyo3 0.28.0 crate/::typesrust-analyzer cargo pyo3 0.28.0 types/::PyBytesrust-analyzer cargo pyo3 0.28.0 types/bytes/PyBytes#::newrust-analyzer cargo pyo3 0.28.0 types/bytes/impl#[PyBytes]new().(py, &self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0.) |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | impl<'py> FromPyObjectrust-analyzer cargo pyo3 0.28.0 conversion/FromPyObject#<'_, 'py> for PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 410 | type Errorrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`FromPyObject<'_, 'py>`]Error# = PyErrrust-analyzer cargo pyo3 0.28.0 err/PyErr#; |
| 411 | |
| 412 | fn extractrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`FromPyObject<'_, 'py>`]extract().(obj: Borrowedrust-analyzer cargo pyo3 0.28.0 instance/Borrowed#<'_, 'py, PyAnyrust-analyzer cargo pyo3 0.28.0 types/any/PyAny#>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`FromPyObject<'_, 'py>`], Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`FromPyObject<'_, 'py>`]::Errorrust-analyzer cargo pyo3 0.28.0 conversion/FromPyObject#Error#> { |
| 413 | let buffer = obj.extractrust-analyzer cargo pyo3 0.28.0 instance/impl#[`Borrowed<'a, 'py, T>`]extract().::<PyBytesWrapperrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytesWrapper#>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 414 | let bytes = Bytesrust-analyzer cargo bytes 1.10.1 bytes/Bytes#::from_ownerrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes]from_owner().(buffer); |
| 415 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][`FromPyObject<'_, 'py>`](bytes)) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /// A wrapper around a PyBuffer that applies a custom destructor that checks if the Python |
| 420 | /// interpreter is still initialized before freeing the buffer memory. |
| 421 | /// |
| 422 | /// This also implements AsRef<[u8]> because that is required for Bytes::from_owner |
| 423 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 424 | struct PyBytesWrapperrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytesWrapper#(PyBufferrust-analyzer cargo pyo3 0.28.0 buffer/PyBuffer#<u8>); |
| 425 | |
| 426 | impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> for PyBytesWrapperrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytesWrapper# { |
| 427 | #[allow(unsafe_code)] |
| 428 | fn as_refrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytesWrapper][`AsRef<[u8]>`]as_ref().(&self) -> &[u8] { |
| 429 | let len = self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytesWrapper#0..item_countrust-analyzer cargo pyo3 0.28.0 buffer/impl#[PyUntypedBuffer]item_count().(); |
| 430 | |
| 431 | let ptr = NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]new().(self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytesWrapper#0..buf_ptrrust-analyzer cargo pyo3 0.28.0 buffer/impl#[PyUntypedBuffer]buf_ptr().() as _).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Expected buffer ptr to be non null"); |
| 432 | |
| 433 | // Safety: |
| 434 | // |
| 435 | // This requires that the data will not be mutated from Python. Sadly, the buffer protocol |
| 436 | // does not uphold this invariant always for us, and the Python user must take care not to |
| 437 | // mutate the provided buffer. |
| 438 | unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(ptr.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().() as *const u8, len) } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | fn validate_bufferrust-analyzer cargo pyo3-bytes 0.6.0 bytes/validate_buffer().(buf: &PyBufferrust-analyzer cargo pyo3 0.28.0 buffer/PyBuffer#<u8>) -> PyResultrust-analyzer cargo pyo3 0.28.0 err/PyResult#<()> { |
| 443 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().buf.is_c_contiguousrust-analyzer cargo pyo3 0.28.0 buffer/impl#[PyUntypedBuffer]is_c_contiguous().() { |
| 444 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PyValueErrorrust-analyzer cargo pyo3 0.28.0 exceptions/PyValueError#::new_errrust-analyzer cargo pyo3 0.28.0 exceptions/impl#[PyValueError]new_err().("Buffer is not C contiguous")); |
| 445 | } |
| 446 | |
| 447 | if buf.stridesrust-analyzer cargo pyo3 0.28.0 buffer/impl#[PyUntypedBuffer]strides().().iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|s| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().s rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialEq<Self>`]ne().1) { |
| 448 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PyValueErrorrust-analyzer cargo pyo3 0.28.0 exceptions/PyValueError#::new_errrust-analyzer cargo pyo3 0.28.0 exceptions/impl#[PyValueError]new_err().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 449 | "strides other than 1 not supported, got: {:?} ", |
| 450 | buf.stridesrust-analyzer cargo pyo3 0.28.0 buffer/impl#[PyUntypedBuffer]strides().() |
| 451 | ))); |
| 452 | } |
| 453 | |
| 454 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 455 | } |
| 456 | |
| 457 | impl<'py> FromPyObjectrust-analyzer cargo pyo3 0.28.0 conversion/FromPyObject#<'_, 'py> for PyBytesWrapperrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytesWrapper# { |
| 458 | type Errorrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytesWrapper][`FromPyObject<'_, 'py>`]Error# = PyErrrust-analyzer cargo pyo3 0.28.0 err/PyErr#; |
| 459 | |
| 460 | fn extractrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytesWrapper][`FromPyObject<'_, 'py>`]extract().(obj: Borrowedrust-analyzer cargo pyo3 0.28.0 instance/Borrowed#<'_, 'py, PyAnyrust-analyzer cargo pyo3 0.28.0 types/any/PyAny#>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytesWrapper][`FromPyObject<'_, 'py>`], Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytesWrapper][`FromPyObject<'_, 'py>`]::Errorrust-analyzer cargo pyo3 0.28.0 conversion/FromPyObject#Error#> { |
| 461 | let buffer = obj.extractrust-analyzer cargo pyo3 0.28.0 instance/impl#[`Borrowed<'a, 'py, T>`]extract().::<PyBufferrust-analyzer cargo pyo3 0.28.0 buffer/PyBuffer#<u8>>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 462 | validate_bufferrust-analyzer cargo pyo3-bytes 0.6.0 bytes/validate_buffer().(&buffer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 463 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytesWrapper][`FromPyObject<'_, 'py>`](buffer)) |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /// This is _mostly_ the same as the upstream [`bytes::Bytes` Debug |
| 468 | /// impl](https://github.com/tokio-rs/bytes/blob/71824b095c4150b3af0776ac158795c00ff9d53f/src/fmt/debug.rs#L6-L37), |
| 469 | /// however we don't use it because that impl doesn't look how the python bytes repr looks; this |
| 470 | /// isn't exactly the same either, as the python repr will switch between `'` and `"` based on the |
| 471 | /// presence of the other in the string, but it's close enough AND we don't have to do a full scan |
| 472 | /// of the bytes to check for that. |
| 473 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for PyBytesrust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes# { |
| 474 | fn fmtrust-analyzer cargo pyo3-bytes 0.6.0 bytes/impl#[PyBytes][Debug]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 475 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("Bytes(b\"")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 476 | for &byte in self.0rust-analyzer cargo pyo3-bytes 0.6.0 bytes/PyBytes#0..as_refrust-analyzer cargo bytes 1.10.1 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().() { |
| 477 | match byte { |
| 478 | // https://doc.rust-lang.org/reference/tokens.html#byte-escapes |
| 479 | b'\\' => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(r"\\")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 480 | b'"' => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("\\\"")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 481 | b'\n' => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(r"\n")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 482 | b'\r' => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(r"\r")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 483 | b'\t' => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(r"\t")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 484 | // printable ASCII |
| 485 | 0x20..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0x7E => f.write_charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'_>`][Write]write_char().(byte as char)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 486 | _ => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\\x{byte:02x}")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 487 | } |
| 488 | } |
| 489 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("\")")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 490 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /// A key for the `__getitem__` method of `PyBytes` - int/slice |
| 495 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(FromPyObjectrust-analyzer cargo pyo3-macros 0.28.0 FromPyObject:)] |
| 496 | enum BytesGetItemKeyrust-analyzer cargo pyo3-bytes 0.6.0 bytes/BytesGetItemKey#<'py> { |
| 497 | /// An integer index |
| 498 | Intrust-analyzer cargo pyo3-bytes 0.6.0 bytes/BytesGetItemKey#Int#(isize), |
| 499 | /// A python slice |
| 500 | Slicerust-analyzer cargo pyo3-bytes 0.6.0 bytes/BytesGetItemKey#Slice#(Boundrust-analyzer cargo pyo3 0.28.0 instance/Bound#<'py, PySlicerust-analyzer cargo pyo3 0.28.0 types/slice/PySlice#>), |
| 501 | } |