Skip to content

Commit

Permalink
PySequence: use usize everywhere
Browse files Browse the repository at this point in the history
See #1667
  • Loading branch information
birkenfeld committed Aug 17, 2021
1 parent 25686a0 commit 279c22c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/conversions/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod min_const_generics {
if seq_len != N {
return Err(invalid_sequence_length(N, seq_len));
}
array_try_from_fn(|idx| seq.get_item(idx as isize).and_then(PyAny::extract))
array_try_from_fn(|idx| seq.get_item(idx).and_then(PyAny::extract))
}

// TODO use std::array::try_from_fn, if that stabilises:
Expand Down
28 changes: 10 additions & 18 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ impl PySequence {
///
/// This is equivalent to the Python expression `len(self)`.
#[inline]
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> PyResult<isize> {
pub fn len(&self) -> PyResult<usize> {
let v = unsafe { ffi::PySequence_Size(self.as_ptr()) };
if v == -1 {
Err(PyErr::api_call_failed(self.py()))
} else {
Ok(v as isize)
Ok(v as usize)
}
}

Expand Down Expand Up @@ -53,9 +52,8 @@ impl PySequence {
/// Returns the result of repeating a sequence object `count` times.
///
/// This is equivalent to the Python expression `self * count`.
/// NB: Python accepts negative counts; it returns an empty Sequence.
#[inline]
pub fn repeat(&self, count: isize) -> PyResult<&PySequence> {
pub fn repeat(&self, count: usize) -> PyResult<&PySequence> {
unsafe {
let ptr = self
.py()
Expand Down Expand Up @@ -85,9 +83,8 @@ impl PySequence {
/// Repeats the sequence object `count` times and updates `self`.
///
/// This is equivalent to the Python statement `self *= count`.
/// NB: Python accepts negative counts; it empties the Sequence.
#[inline]
pub fn in_place_repeat(&self, count: isize) -> PyResult<()> {
pub fn in_place_repeat(&self, count: usize) -> PyResult<()> {
unsafe {
let ptr = ffi::PySequence_InPlaceRepeat(self.as_ptr(), count as Py_ssize_t);
if ptr.is_null() {
Expand All @@ -102,7 +99,7 @@ impl PySequence {
///
/// This is equivalent to the Python expression `self[index]`.
#[inline]
pub fn get_item(&self, index: isize) -> PyResult<&PyAny> {
pub fn get_item(&self, index: usize) -> PyResult<&PyAny> {
unsafe {
self.py()
.from_owned_ptr_or_err(ffi::PySequence_GetItem(self.as_ptr(), index as Py_ssize_t))
Expand All @@ -113,7 +110,7 @@ impl PySequence {
///
/// This is equivalent to the Python expression `self[begin:end]`.
#[inline]
pub fn get_slice(&self, begin: isize, end: isize) -> PyResult<&PyAny> {
pub fn get_slice(&self, begin: usize, end: usize) -> PyResult<&PyAny> {
unsafe {
self.py().from_owned_ptr_or_err(ffi::PySequence_GetSlice(
self.as_ptr(),
Expand All @@ -127,7 +124,7 @@ impl PySequence {
///
/// This is equivalent to the Python statement `self[i] = v`.
#[inline]
pub fn set_item<I>(&self, i: isize, item: I) -> PyResult<()>
pub fn set_item<I>(&self, i: usize, item: I) -> PyResult<()>
where
I: ToBorrowedObject,
{
Expand All @@ -145,7 +142,7 @@ impl PySequence {
///
/// This is equivalent to the Python statement `del self[i]`.
#[inline]
pub fn del_item(&self, i: isize) -> PyResult<()> {
pub fn del_item(&self, i: usize) -> PyResult<()> {
unsafe {
err::error_on_minusone(
self.py(),
Expand All @@ -158,7 +155,7 @@ impl PySequence {
///
/// This is equivalent to the Python statement `self[i1:i2] = v`.
#[inline]
pub fn set_slice(&self, i1: isize, i2: isize, v: &PyAny) -> PyResult<()> {
pub fn set_slice(&self, i1: usize, i2: usize, v: &PyAny) -> PyResult<()> {
unsafe {
err::error_on_minusone(
self.py(),
Expand All @@ -176,7 +173,7 @@ impl PySequence {
///
/// This is equivalent to the Python statement `del self[i1:i2]`.
#[inline]
pub fn del_slice(&self, i1: isize, i2: isize) -> PyResult<()> {
pub fn del_slice(&self, i1: usize, i2: usize) -> PyResult<()> {
unsafe {
err::error_on_minusone(
self.py(),
Expand Down Expand Up @@ -404,11 +401,6 @@ mod tests {
assert_eq!(3, seq.get_item(3).unwrap().extract::<i32>().unwrap());
assert_eq!(5, seq.get_item(4).unwrap().extract::<i32>().unwrap());
assert_eq!(8, seq.get_item(5).unwrap().extract::<i32>().unwrap());
assert_eq!(8, seq.get_item(-1).unwrap().extract::<i32>().unwrap());
assert_eq!(5, seq.get_item(-2).unwrap().extract::<i32>().unwrap());
assert_eq!(3, seq.get_item(-3).unwrap().extract::<i32>().unwrap());
assert_eq!(2, seq.get_item(-4).unwrap().extract::<i32>().unwrap());
assert_eq!(1, seq.get_item(-5).unwrap().extract::<i32>().unwrap());
assert!(seq.get_item(10).is_err());
});
}
Expand Down

0 comments on commit 279c22c

Please sign in to comment.