Repository navigation
generic and efficiency way to consume iterable object - #2284
Conversation
| pub fn try_iterable_map<F, T, R>( | ||
| vm: &VirtualMachine, | ||
| obj: &PyObjectRef, | ||
| mut f: F, | ||
| ) -> PyResult<Vec<R>> | ||
| where | ||
| T: TryFromObject, | ||
| F: FnMut(T) -> PyResult<R>, | ||
| { |
|
Looks good! I think it would make sense to put this as a method on |
|
I think I stacked here, CPython allows create the bytes from an iterable object like a list, and inside the list you could put the object that has This cause the problem if we map the list so the list is borrowed, and when I think we may have to go back to extract all the elements from the list before try to convert the items. (CPython is doing like lazy-evaluation). Or any idea that help? |
|
Or we need to do the map manually, so we clone the item out, free the lock, do the convert. |
|
When there is difference, following CPython way is almost the best. |
|
I moved the mutable function from bytesinner to bytearray for better control of the borrowing. |
| pub fn map_elements<F, T, R>(&self, obj: &PyObjectRef, mut f: F) -> PyResult<PyResult<Vec<R>>> | ||
| where | ||
| T: TryFromObject, | ||
| F: FnMut(T) -> PyResult<R>, | ||
| { | ||
| self.map_iterable_object(obj, |elem| f(T::try_from_object(self, elem)?)) | ||
| } |
There was a problem hiding this comment.
This function not been use yet, maybe remove it?
There was a problem hiding this comment.
Generally, yes. having unused code is a sort of cost. Do you have plan to use it in other patch?
|
#2125 fixed |
| { | ||
| let cap = length_hint(vm, iter_obj.clone())?.unwrap_or(0); | ||
| // TODO: fix extend to do this check (?), see test_extend in Lib/test/list_tests.py, | ||
| // https://github.com/python/cpython/blob/master/Objects/listobject.c#L934-L940 |
There was a problem hiding this comment.
link to cpython master will be broken one day. link to specific tag will be better.
| let cap = length_hint(vm, iter_obj.clone())?.unwrap_or(0); | ||
| // TODO: fix extend to do this check (?), see test_extend in Lib/test/list_tests.py, | ||
| // https://github.com/python/cpython/blob/master/Objects/listobject.c#L934-L940 | ||
| if cap >= isize::max_value() as usize { |
There was a problem hiding this comment.
I think this is sys.maxsize but just found there is no way to access to the const in rust side.
No description provided.