-
Notifications
You must be signed in to change notification settings - Fork 1.3k
generic and efficiency way to consume iterable object #2284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
vm/src/pyobject.rs
Outdated
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>, | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try_map_iter?
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. |
vm/src/vm.rs
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function not been use yet, maybe remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, yes. having unused code is a sort of cost. Do you have plan to use it in other patch?
#2125 fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
vm/src/iterator.rs
Outdated
{ | ||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is sys.maxsize
but just found there is no way to access to the const in rust side.
No description provided.