use pyo3::{
basic::CompareOp,
exceptions::PyValueError,
prelude::{Bound, *},
types::PyBytes,
};
use crate::bytes::strategy::compact::Compact;
use crate::bytes::strategy::shared::Shared;
use crate::python::{py_check_alloc, py_str_contains, py_str_getitem, py_str_richcmp};
use crate::utf8_buf::char_offset_to_byte;
type SharedUtf8Bytes = super::Utf8Bytes<Shared>;
type CompactUtf8Bytes = super::Utf8Bytes<Compact>;
#[derive(Debug)]
#[pyclass]
struct Utf8CharIter {
chars: Vec<char>,
index: usize,
}
#[pymethods]
impl Utf8CharIter {
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
slf
}
fn __next__(&mut self) -> Option<String> {
if self.index < self.chars.len() {
let ch = self.chars[self.index];
self.index += 1;
Some(ch.to_string())
} else {
None
}
}
}
#[pyclass(name = "Utf8Bytes", skip_from_py_object)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct PySharedUtf8Bytes {
inner: SharedUtf8Bytes,
}
impl From<SharedUtf8Bytes> for PySharedUtf8Bytes {
fn from(inner: SharedUtf8Bytes) -> Self {
Self { inner }
}
}
impl AsRef<str> for PySharedUtf8Bytes {
fn as_ref(&self) -> &str {
self.inner.as_str()
}
}
impl AsRef<[u8]> for PySharedUtf8Bytes {
fn as_ref(&self) -> &[u8] {
self.inner.as_str().as_bytes()
}
}
#[pymethods]
impl PySharedUtf8Bytes {
#[new]
fn new_python() -> Self {
Self {
inner: SharedUtf8Bytes::new(),
}
}
#[staticmethod]
#[pyo3(name = "from_str")]
fn __python_from_str(s: &str) -> PyResult<Self> {
py_check_alloc(s.len())?;
Ok(Self {
inner: SharedUtf8Bytes::from(s),
})
}
fn __str__(&self) -> &str {
self.inner.as_str()
}
fn __repr__(&self) -> PyResult<String> {
py_check_alloc(
self
.inner
.as_str()
.len()
.saturating_mul(6)
.saturating_add(64),
)?;
Ok(format!("{:?}", self.inner))
}
fn __len__(&self) -> usize {
self.inner.as_str().chars().count()
}
fn __bool__(&self) -> bool {
!self.inner.is_empty()
}
#[allow(non_upper_case_globals)]
const __hash__: Option<Py<PyAny>> = None;
fn __contains__(&self, item: &Bound<'_, PyAny>) -> PyResult<bool> {
py_str_contains(self.inner.as_str(), item)
}
fn __getitem__(&self, index: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
py_str_getitem(self.inner.as_str(), index)
}
fn __richcmp__(&self, other: &Bound<'_, PyAny>, op: CompareOp) -> PyResult<Py<PyAny>> {
py_str_richcmp(self.inner.as_str(), other, op)
}
#[pyo3(name = "split_to")]
fn __python_split_to(&mut self, at: usize) -> PyResult<Self> {
let byte_at = char_offset_to_byte(self.inner.as_str(), at)
.ok_or_else(|| PyValueError::new_err(format!("split index {} out of range", at)))?;
self
.inner
.try_split_to(byte_at)
.map(Into::into)
.map_err(|e| PyValueError::new_err(e.to_string()))
}
#[pyo3(name = "split_off")]
fn __python_split_off(&mut self, at: usize) -> PyResult<Self> {
let byte_at = char_offset_to_byte(self.inner.as_str(), at)
.ok_or_else(|| PyValueError::new_err(format!("split index {} out of range", at)))?;
self
.inner
.try_split_off(byte_at)
.map(Into::into)
.map_err(|e| PyValueError::new_err(e.to_string()))
}
#[pyo3(name = "slice")]
fn __python_slice(&self, start: usize, end: usize) -> PyResult<Self> {
let s = self.inner.as_str();
let start = char_offset_to_byte(s, start)
.ok_or_else(|| PyValueError::new_err(format!("slice index {} out of range", start)))?;
let end = char_offset_to_byte(s, end)
.ok_or_else(|| PyValueError::new_err(format!("slice index {} out of range", end)))?;
self
.inner
.try_slice(start..end)
.map(Into::into)
.map_err(|e| PyValueError::new_err(e.to_string()))
}
fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<Utf8CharIter>> {
let char_count = slf.inner.as_str().chars().count();
py_check_alloc(char_count.saturating_mul(core::mem::size_of::<char>()))?;
let chars: Vec<char> = slf.inner.as_str().chars().collect();
Py::new(slf.py(), Utf8CharIter { chars, index: 0 })
}
fn __bytes__<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
PyBytes::new(py, self.inner.as_str().as_bytes())
}
#[pyo3(name = "is_inline")]
fn __python_is_inline(&self) -> bool {
self.inner.is_inline()
}
#[pyo3(name = "is_heap")]
fn __python_is_heap(&self) -> bool {
self.inner.is_heap()
}
#[pyo3(name = "byte_len")]
fn __python_byte_len(&self) -> usize {
self.inner.as_str().len()
}
#[pyo3(name = "__copy__")]
fn __python_copy(&self) -> Self {
self.clone()
}
#[pyo3(name = "__deepcopy__")]
fn __python_deepcopy(&self, _memo: &Bound<'_, PyAny>) -> Self {
self.clone()
}
fn __reduce__(slf: PyRef<'_, Self>, py: Python<'_>) -> PyResult<(Py<PyAny>, (String,))> {
let cls = py.get_type::<Self>();
let from_str = cls.getattr("from_str")?;
py_check_alloc(slf.inner.as_str().len())?;
Ok((from_str.unbind(), (slf.inner.as_str().to_string(),)))
}
#[pyo3(name = "remaining")]
fn __python_remaining(&self) -> usize {
use bytes::Buf;
self.inner.as_inner().remaining()
}
#[pyo3(name = "advance")]
fn __python_advance(&mut self, cnt: usize) -> PyResult<()> {
use bytes::Buf;
if cnt > self.inner.as_inner().remaining() {
return Err(pyo3::exceptions::PyBufferError::new_err(format!(
"cannot advance past remaining: {} > {}",
cnt,
self.inner.as_inner().remaining()
)));
}
crate::python::validate_utf8_advance(self, cnt)?;
self.inner.inner.advance(cnt);
Ok(())
}
#[pyo3(name = "get_u8")]
fn __python_get_u8(&mut self) -> PyResult<u8> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 1 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u8",
));
}
crate::python::validate_utf8_advance(self, 1)?;
Ok(self.inner.inner.get_u8())
}
#[pyo3(name = "get_i8")]
fn __python_get_i8(&mut self) -> PyResult<i8> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 1 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i8",
));
}
crate::python::validate_utf8_advance(self, 1)?;
Ok(self.inner.inner.get_i8())
}
#[pyo3(name = "get_u16")]
fn __python_get_u16(&mut self) -> PyResult<u16> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 2 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u16",
));
}
crate::python::validate_utf8_advance(self, 2)?;
Ok(self.inner.inner.get_u16())
}
#[pyo3(name = "get_u16_le")]
fn __python_get_u16_le(&mut self) -> PyResult<u16> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 2 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u16",
));
}
crate::python::validate_utf8_advance(self, 2)?;
Ok(self.inner.inner.get_u16_le())
}
#[pyo3(name = "get_i16")]
fn __python_get_i16(&mut self) -> PyResult<i16> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 2 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i16",
));
}
crate::python::validate_utf8_advance(self, 2)?;
Ok(self.inner.inner.get_i16())
}
#[pyo3(name = "get_i16_le")]
fn __python_get_i16_le(&mut self) -> PyResult<i16> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 2 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i16",
));
}
crate::python::validate_utf8_advance(self, 2)?;
Ok(self.inner.inner.get_i16_le())
}
#[pyo3(name = "get_u32")]
fn __python_get_u32(&mut self) -> PyResult<u32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_u32())
}
#[pyo3(name = "get_u32_le")]
fn __python_get_u32_le(&mut self) -> PyResult<u32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_u32_le())
}
#[pyo3(name = "get_i32")]
fn __python_get_i32(&mut self) -> PyResult<i32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_i32())
}
#[pyo3(name = "get_i32_le")]
fn __python_get_i32_le(&mut self) -> PyResult<i32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_i32_le())
}
#[pyo3(name = "get_u64")]
fn __python_get_u64(&mut self) -> PyResult<u64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_u64())
}
#[pyo3(name = "get_u64_le")]
fn __python_get_u64_le(&mut self) -> PyResult<u64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_u64_le())
}
#[pyo3(name = "get_i64")]
fn __python_get_i64(&mut self) -> PyResult<i64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_i64())
}
#[pyo3(name = "get_i64_le")]
fn __python_get_i64_le(&mut self) -> PyResult<i64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_i64_le())
}
#[pyo3(name = "get_u128")]
fn __python_get_u128(&mut self) -> PyResult<u128> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 16 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u128",
));
}
crate::python::validate_utf8_advance(self, 16)?;
Ok(self.inner.inner.get_u128())
}
#[pyo3(name = "get_u128_le")]
fn __python_get_u128_le(&mut self) -> PyResult<u128> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 16 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u128",
));
}
crate::python::validate_utf8_advance(self, 16)?;
Ok(self.inner.inner.get_u128_le())
}
#[pyo3(name = "get_i128")]
fn __python_get_i128(&mut self) -> PyResult<i128> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 16 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i128",
));
}
crate::python::validate_utf8_advance(self, 16)?;
Ok(self.inner.inner.get_i128())
}
#[pyo3(name = "get_i128_le")]
fn __python_get_i128_le(&mut self) -> PyResult<i128> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 16 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i128",
));
}
crate::python::validate_utf8_advance(self, 16)?;
Ok(self.inner.inner.get_i128_le())
}
#[pyo3(name = "get_f32")]
fn __python_get_f32(&mut self) -> PyResult<f32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for f32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_f32())
}
#[pyo3(name = "get_f32_le")]
fn __python_get_f32_le(&mut self) -> PyResult<f32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for f32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_f32_le())
}
#[pyo3(name = "get_f64")]
fn __python_get_f64(&mut self) -> PyResult<f64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for f64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_f64())
}
#[pyo3(name = "get_f64_le")]
fn __python_get_f64_le(&mut self) -> PyResult<f64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for f64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_f64_le())
}
#[pyo3(name = "get_uint")]
fn __python_get_uint(&mut self, nbytes: &Bound<'_, PyAny>) -> PyResult<u64> {
use bytes::Buf;
let nbytes = crate::python::py_integer_width(nbytes)?;
if self.inner.as_inner().remaining() < nbytes {
return Err(pyo3::exceptions::PyBufferError::new_err("not enough data"));
}
crate::python::validate_utf8_advance(self, nbytes)?;
Ok(self.inner.inner.get_uint(nbytes))
}
#[pyo3(name = "get_uint_le")]
fn __python_get_uint_le(&mut self, nbytes: &Bound<'_, PyAny>) -> PyResult<u64> {
use bytes::Buf;
let nbytes = crate::python::py_integer_width(nbytes)?;
if self.inner.as_inner().remaining() < nbytes {
return Err(pyo3::exceptions::PyBufferError::new_err("not enough data"));
}
crate::python::validate_utf8_advance(self, nbytes)?;
Ok(self.inner.inner.get_uint_le(nbytes))
}
#[pyo3(name = "get_int")]
fn __python_get_int(&mut self, nbytes: &Bound<'_, PyAny>) -> PyResult<i64> {
use bytes::Buf;
let nbytes = crate::python::py_integer_width(nbytes)?;
if self.inner.as_inner().remaining() < nbytes {
return Err(pyo3::exceptions::PyBufferError::new_err("not enough data"));
}
crate::python::validate_utf8_advance(self, nbytes)?;
Ok(self.inner.inner.get_int(nbytes))
}
#[pyo3(name = "get_int_le")]
fn __python_get_int_le(&mut self, nbytes: &Bound<'_, PyAny>) -> PyResult<i64> {
use bytes::Buf;
let nbytes = crate::python::py_integer_width(nbytes)?;
if self.inner.as_inner().remaining() < nbytes {
return Err(pyo3::exceptions::PyBufferError::new_err("not enough data"));
}
crate::python::validate_utf8_advance(self, nbytes)?;
Ok(self.inner.inner.get_int_le(nbytes))
}
}
#[pyclass(name = "Utf8Bytes", skip_from_py_object)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct PyCompactUtf8Bytes {
inner: CompactUtf8Bytes,
}
impl From<CompactUtf8Bytes> for PyCompactUtf8Bytes {
fn from(inner: CompactUtf8Bytes) -> Self {
Self { inner }
}
}
impl AsRef<str> for PyCompactUtf8Bytes {
fn as_ref(&self) -> &str {
self.inner.as_str()
}
}
impl AsRef<[u8]> for PyCompactUtf8Bytes {
fn as_ref(&self) -> &[u8] {
self.inner.as_str().as_bytes()
}
}
#[pymethods]
impl PyCompactUtf8Bytes {
#[new]
fn new_python() -> Self {
Self {
inner: CompactUtf8Bytes::new(),
}
}
#[staticmethod]
#[pyo3(name = "from_str")]
fn __python_from_str(s: &str) -> PyResult<Self> {
py_check_alloc(s.len())?;
Ok(Self {
inner: CompactUtf8Bytes::from(s),
})
}
fn __str__(&self) -> &str {
self.inner.as_str()
}
fn __repr__(&self) -> PyResult<String> {
py_check_alloc(
self
.inner
.as_str()
.len()
.saturating_mul(6)
.saturating_add(64),
)?;
Ok(format!("{:?}", self.inner))
}
fn __len__(&self) -> usize {
self.inner.as_str().chars().count()
}
fn __bool__(&self) -> bool {
!self.inner.is_empty()
}
#[allow(non_upper_case_globals)]
const __hash__: Option<Py<PyAny>> = None;
fn __contains__(&self, item: &Bound<'_, PyAny>) -> PyResult<bool> {
py_str_contains(self.inner.as_str(), item)
}
fn __getitem__(&self, index: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
py_str_getitem(self.inner.as_str(), index)
}
fn __richcmp__(&self, other: &Bound<'_, PyAny>, op: CompareOp) -> PyResult<Py<PyAny>> {
py_str_richcmp(self.inner.as_str(), other, op)
}
#[pyo3(name = "split_to")]
fn __python_split_to(&mut self, at: usize) -> PyResult<Self> {
let byte_at = char_offset_to_byte(self.inner.as_str(), at)
.ok_or_else(|| PyValueError::new_err(format!("split index {} out of range", at)))?;
self
.inner
.try_split_to(byte_at)
.map(Into::into)
.map_err(|e| PyValueError::new_err(e.to_string()))
}
#[pyo3(name = "split_off")]
fn __python_split_off(&mut self, at: usize) -> PyResult<Self> {
let byte_at = char_offset_to_byte(self.inner.as_str(), at)
.ok_or_else(|| PyValueError::new_err(format!("split index {} out of range", at)))?;
self
.inner
.try_split_off(byte_at)
.map(Into::into)
.map_err(|e| PyValueError::new_err(e.to_string()))
}
#[pyo3(name = "slice")]
fn __python_slice(&self, start: usize, end: usize) -> PyResult<Self> {
let s = self.inner.as_str();
let start = char_offset_to_byte(s, start)
.ok_or_else(|| PyValueError::new_err(format!("slice index {} out of range", start)))?;
let end = char_offset_to_byte(s, end)
.ok_or_else(|| PyValueError::new_err(format!("slice index {} out of range", end)))?;
self
.inner
.try_slice(start..end)
.map(Into::into)
.map_err(|e| PyValueError::new_err(e.to_string()))
}
fn __iter__(slf: PyRef<'_, Self>) -> PyResult<Py<Utf8CharIter>> {
let char_count = slf.inner.as_str().chars().count();
py_check_alloc(char_count.saturating_mul(core::mem::size_of::<char>()))?;
let chars: Vec<char> = slf.inner.as_str().chars().collect();
Py::new(slf.py(), Utf8CharIter { chars, index: 0 })
}
fn __bytes__<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
PyBytes::new(py, self.inner.as_str().as_bytes())
}
#[pyo3(name = "is_inline")]
fn __python_is_inline(&self) -> bool {
self.inner.is_inline()
}
#[pyo3(name = "is_heap")]
fn __python_is_heap(&self) -> bool {
self.inner.is_heap()
}
#[pyo3(name = "byte_len")]
fn __python_byte_len(&self) -> usize {
self.inner.as_str().len()
}
#[pyo3(name = "__copy__")]
fn __python_copy(&self) -> Self {
self.clone()
}
#[pyo3(name = "__deepcopy__")]
fn __python_deepcopy(&self, _memo: &Bound<'_, PyAny>) -> Self {
self.clone()
}
fn __reduce__(slf: PyRef<'_, Self>, py: Python<'_>) -> PyResult<(Py<PyAny>, (String,))> {
let cls = py.get_type::<Self>();
let from_str = cls.getattr("from_str")?;
py_check_alloc(slf.inner.as_str().len())?;
Ok((from_str.unbind(), (slf.inner.as_str().to_string(),)))
}
#[pyo3(name = "remaining")]
fn __python_remaining(&self) -> usize {
use bytes::Buf;
self.inner.as_inner().remaining()
}
#[pyo3(name = "advance")]
fn __python_advance(&mut self, cnt: usize) -> PyResult<()> {
use bytes::Buf;
if cnt > self.inner.as_inner().remaining() {
return Err(pyo3::exceptions::PyBufferError::new_err(format!(
"cannot advance past remaining: {} > {}",
cnt,
self.inner.as_inner().remaining()
)));
}
crate::python::validate_utf8_advance(self, cnt)?;
self.inner.inner.advance(cnt);
Ok(())
}
#[pyo3(name = "get_u8")]
fn __python_get_u8(&mut self) -> PyResult<u8> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 1 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u8",
));
}
crate::python::validate_utf8_advance(self, 1)?;
Ok(self.inner.inner.get_u8())
}
#[pyo3(name = "get_i8")]
fn __python_get_i8(&mut self) -> PyResult<i8> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 1 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i8",
));
}
crate::python::validate_utf8_advance(self, 1)?;
Ok(self.inner.inner.get_i8())
}
#[pyo3(name = "get_u16")]
fn __python_get_u16(&mut self) -> PyResult<u16> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 2 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u16",
));
}
crate::python::validate_utf8_advance(self, 2)?;
Ok(self.inner.inner.get_u16())
}
#[pyo3(name = "get_u16_le")]
fn __python_get_u16_le(&mut self) -> PyResult<u16> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 2 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u16",
));
}
crate::python::validate_utf8_advance(self, 2)?;
Ok(self.inner.inner.get_u16_le())
}
#[pyo3(name = "get_i16")]
fn __python_get_i16(&mut self) -> PyResult<i16> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 2 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i16",
));
}
crate::python::validate_utf8_advance(self, 2)?;
Ok(self.inner.inner.get_i16())
}
#[pyo3(name = "get_i16_le")]
fn __python_get_i16_le(&mut self) -> PyResult<i16> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 2 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i16",
));
}
crate::python::validate_utf8_advance(self, 2)?;
Ok(self.inner.inner.get_i16_le())
}
#[pyo3(name = "get_u32")]
fn __python_get_u32(&mut self) -> PyResult<u32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_u32())
}
#[pyo3(name = "get_u32_le")]
fn __python_get_u32_le(&mut self) -> PyResult<u32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_u32_le())
}
#[pyo3(name = "get_i32")]
fn __python_get_i32(&mut self) -> PyResult<i32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_i32())
}
#[pyo3(name = "get_i32_le")]
fn __python_get_i32_le(&mut self) -> PyResult<i32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_i32_le())
}
#[pyo3(name = "get_u64")]
fn __python_get_u64(&mut self) -> PyResult<u64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_u64())
}
#[pyo3(name = "get_u64_le")]
fn __python_get_u64_le(&mut self) -> PyResult<u64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_u64_le())
}
#[pyo3(name = "get_i64")]
fn __python_get_i64(&mut self) -> PyResult<i64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_i64())
}
#[pyo3(name = "get_i64_le")]
fn __python_get_i64_le(&mut self) -> PyResult<i64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_i64_le())
}
#[pyo3(name = "get_u128")]
fn __python_get_u128(&mut self) -> PyResult<u128> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 16 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u128",
));
}
crate::python::validate_utf8_advance(self, 16)?;
Ok(self.inner.inner.get_u128())
}
#[pyo3(name = "get_u128_le")]
fn __python_get_u128_le(&mut self) -> PyResult<u128> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 16 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for u128",
));
}
crate::python::validate_utf8_advance(self, 16)?;
Ok(self.inner.inner.get_u128_le())
}
#[pyo3(name = "get_i128")]
fn __python_get_i128(&mut self) -> PyResult<i128> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 16 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i128",
));
}
crate::python::validate_utf8_advance(self, 16)?;
Ok(self.inner.inner.get_i128())
}
#[pyo3(name = "get_i128_le")]
fn __python_get_i128_le(&mut self) -> PyResult<i128> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 16 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for i128",
));
}
crate::python::validate_utf8_advance(self, 16)?;
Ok(self.inner.inner.get_i128_le())
}
#[pyo3(name = "get_f32")]
fn __python_get_f32(&mut self) -> PyResult<f32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for f32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_f32())
}
#[pyo3(name = "get_f32_le")]
fn __python_get_f32_le(&mut self) -> PyResult<f32> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 4 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for f32",
));
}
crate::python::validate_utf8_advance(self, 4)?;
Ok(self.inner.inner.get_f32_le())
}
#[pyo3(name = "get_f64")]
fn __python_get_f64(&mut self) -> PyResult<f64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for f64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_f64())
}
#[pyo3(name = "get_f64_le")]
fn __python_get_f64_le(&mut self) -> PyResult<f64> {
use bytes::Buf;
if self.inner.as_inner().remaining() < 8 {
return Err(pyo3::exceptions::PyBufferError::new_err(
"not enough data for f64",
));
}
crate::python::validate_utf8_advance(self, 8)?;
Ok(self.inner.inner.get_f64_le())
}
#[pyo3(name = "get_uint")]
fn __python_get_uint(&mut self, nbytes: &Bound<'_, PyAny>) -> PyResult<u64> {
use bytes::Buf;
let nbytes = crate::python::py_integer_width(nbytes)?;
if self.inner.as_inner().remaining() < nbytes {
return Err(pyo3::exceptions::PyBufferError::new_err("not enough data"));
}
crate::python::validate_utf8_advance(self, nbytes)?;
Ok(self.inner.inner.get_uint(nbytes))
}
#[pyo3(name = "get_uint_le")]
fn __python_get_uint_le(&mut self, nbytes: &Bound<'_, PyAny>) -> PyResult<u64> {
use bytes::Buf;
let nbytes = crate::python::py_integer_width(nbytes)?;
if self.inner.as_inner().remaining() < nbytes {
return Err(pyo3::exceptions::PyBufferError::new_err("not enough data"));
}
crate::python::validate_utf8_advance(self, nbytes)?;
Ok(self.inner.inner.get_uint_le(nbytes))
}
#[pyo3(name = "get_int")]
fn __python_get_int(&mut self, nbytes: &Bound<'_, PyAny>) -> PyResult<i64> {
use bytes::Buf;
let nbytes = crate::python::py_integer_width(nbytes)?;
if self.inner.as_inner().remaining() < nbytes {
return Err(pyo3::exceptions::PyBufferError::new_err("not enough data"));
}
crate::python::validate_utf8_advance(self, nbytes)?;
Ok(self.inner.inner.get_int(nbytes))
}
#[pyo3(name = "get_int_le")]
fn __python_get_int_le(&mut self, nbytes: &Bound<'_, PyAny>) -> PyResult<i64> {
use bytes::Buf;
let nbytes = crate::python::py_integer_width(nbytes)?;
if self.inner.as_inner().remaining() < nbytes {
return Err(pyo3::exceptions::PyBufferError::new_err("not enough data"));
}
crate::python::validate_utf8_advance(self, nbytes)?;
Ok(self.inner.inner.get_int_le(nbytes))
}
}