Skip to content

Commit

Permalink
Add PyList.reverse().
Browse files Browse the repository at this point in the history
  • Loading branch information
chr1sj0nes committed Feb 17, 2019
1 parent b01592e commit af305a1
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ impl PyList {
/// Sorts the list in-place. Equivalent to python `l.sort()`
pub fn sort(&self) -> PyResult<()> {
unsafe { err::error_on_minusone(self.py(), ffi::PyList_Sort(self.as_ptr())) }

/// Reverses the list in-place. Equivalent to python `l.reverse()`
pub fn reverse(&self) -> PyResult<()> {
unsafe { err::error_on_minusone(self.py(), ffi::PyList_Reverse(self.as_ptr())) }
}
}

Expand Down Expand Up @@ -403,4 +407,20 @@ mod test {
assert_eq!(5, list.get_item(2).extract::<i32>().unwrap());
assert_eq!(7, list.get_item(3).extract::<i32>().unwrap());
}

fn test_reverse() {
let gil = Python::acquire_gil();
let py = gil.python();
let v = vec![2, 3, 5, 7];
let list = PyList::new(py, &v);
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
assert_eq!(3, list.get_item(1).extract::<i32>().unwrap());
assert_eq!(5, list.get_item(2).extract::<i32>().unwrap());
assert_eq!(7, list.get_item(3).extract::<i32>().unwrap());
list.reverse().unwrap();
assert_eq!(7, list.get_item(0).extract::<i32>().unwrap());
assert_eq!(5, list.get_item(1).extract::<i32>().unwrap());
assert_eq!(3, list.get_item(2).extract::<i32>().unwrap());
assert_eq!(2, list.get_item(3).extract::<i32>().unwrap());
}
}

0 comments on commit af305a1

Please sign in to comment.