| 1866 | } |
| 1867 | |
| 1868 | pub fn map_iterable_object<F, R>(&self, obj: &PyObject, mut f: F) -> PyResult<PyResult<Vec<R>>> |
| 1869 | where |
| 1870 | F: FnMut(PyObjectRef) -> PyResult<R>, |
| 1871 | { |
| 1872 | match_class!(match obj { |
| 1873 | ref l @ PyList => { |
| 1874 | let mut i: usize = 0; |
| 1875 | let mut results = Vec::with_capacity(l.borrow_vec().len()); |
| 1876 | loop { |
| 1877 | let elem = { |
| 1878 | let elements = &*l.borrow_vec(); |
| 1879 | if i >= elements.len() { |
| 1880 | results.shrink_to_fit(); |
| 1881 | return Ok(Ok(results)); |
| 1882 | } else { |
| 1883 | elements[i].clone() |
| 1884 | } |
| 1885 | // free the lock |
| 1886 | }; |
| 1887 | match f(elem) { |
| 1888 | Ok(result) => results.push(result), |
| 1889 | Err(err) => return Ok(Err(err)), |
| 1890 | } |
| 1891 | i += 1; |
| 1892 | } |
| 1893 | } |
| 1894 | ref t @ PyTuple => Ok(t.iter().cloned().map(f).collect()), |
| 1895 | // TODO: put internal iterable type |
| 1896 | obj => { |
| 1897 | Ok(self.map_py_iter(obj, f)) |
| 1898 | } |
| 1899 | }) |
| 1900 | } |
| 1901 | |
| 1902 | fn map_py_iter<F, R>(&self, value: &PyObject, mut f: F) -> PyResult<Vec<R>> |
| 1903 | where |