-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Tracking Issue for Extend::{extend_one,extend_reserve} #72631
Comments
Preserving a relevant concern from @SimonSapin in #72162 (comment):
|
Additional concern:
|
With arrays implementing the IntoIterator trait, I'm not sure |
It's not just how nice it looks, but also simpler implementation, like |
@cuviper can't this be also solved with specialization? Adding special code for |
Yeah, I suppose specialization could do that too. Note that only |
Good point! |
Summarizing some real-time discussion from just now:
For (I'd still prefer to have it as a free function that can't be overridden, rather than a default method, but with the above documentation and default impl I'd just be -0 on it and wouldn't oppose adding it.) |
So, returning a hint
You mean by using that hint trick in the free function? I could do that. Actually, if we go that route, we can also let (Note: |
That's a good point.
So it implies that it's kind of buggy behavior to have an iterator that doesn't return its lower bound. At the very least it leaves a bad taste. It would be nicer if we just passed in a single iterator where a call to |
Shoulld |
I would also like a pub trait Push {
type Item;
fn push<'a>(&'a mut self, item: Self::Item) -> &'a Self::Item;
}
pub trait TryPush {
type Item;
fn try_push<'a>(&'a mut self, item: Self::Item) -> Result<&'a Self::Item>;
}
pub trait TryExtend {
type Item;
fn try_extend<Iter: IntoIterator<Item = Self::Item>>(&mut self, iter: Iter) -> Result<()>;
} |
|
Oh It was just a quite and dirty example, my main idea is I don't see why we can't have a trait for Push one item on alloc collection. Maybe split
So maybe: pub trait Push {
type Item;
type Output;
fn push<'a>(&'a mut self, item: Self::Item) -> Self::Output;
}
pub trait TryPush {
type Item;
type Output;
// the error should return the item cause it didn't consume it
fn try_push<'a>(&'a mut self, item: Self::Item) -> Result<Self::Output, ErrorFoo<Self::Item>>;
} I don't know |
A GAT API for pushing to #![feature(entry_insert, generic_associated_types)]
use std::collections::HashMap;
use std::hash::{BuildHasher, Hash};
pub trait Push {
type Item;
type Output<'a>
where
Self: 'a;
fn push<'a>(&'a mut self, item: Self::Item) -> Self::Output<'a>;
}
impl<K, V, S> Push for HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher,
{
type Item = (K, V);
type Output<'a>
where
Self: 'a,
= &'a mut V;
fn push<'a>(&'a mut self, (key, value): (K, V)) -> &'a mut V {
self.entry(key).insert(value).into_mut()
}
} |
I'm writing something that uses size hint similar to iterators and was wondering how it works in I'm thinking about these scenarios:
I'm still unsure about the best solution but if there are people smarter than me interested I'd like to know what they are thinking. |
@Kixunil I would like to note that repeatedly calling |
@qm3ster I believe all such cases can be solved using |
Of course! |
Ah, finally a good case when it's needed! |
Perhaps another solution is some kind of |
For the record, I've just ran some simple benchmarks against example::extend_one:
push rbx
mov rbx, rdi
mov rsi, qword ptr [rdi + 16]
cmp rsi, qword ptr [rdi + 8]
jne .LBB3_2
mov rdi, rbx
call alloc::raw_vec::RawVec<T,A>::reserve_for_push
mov rsi, qword ptr [rbx + 16]
.LBB3_2:
mov rax, qword ptr [rbx]
mov byte ptr [rax + rsi], 0
inc rsi
mov qword ptr [rbx + 16], rsi
pop rbx
ret
example::extend_some:
push rbx
mov rbx, rdi
mov rsi, qword ptr [rdi + 16]
cmp qword ptr [rdi + 8], rsi
je .LBB4_1
.LBB4_2:
mov rax, qword ptr [rbx]
mov byte ptr [rax + rsi], 0
inc rsi
mov qword ptr [rbx + 16], rsi
pop rbx
ret
.LBB4_1:
mov edx, 1
mov rdi, rbx
call alloc::raw_vec::RawVec<T,A>::reserve::do_reserve_and_handle
mov rsi, qword ptr [rbx + 16]
jmp .LBB4_2 My judgement from reading the raw_vec code is that |
This is a tracking issue for the trait methods
Extend::extend_one
andextend_reserve
.The feature gate for the issue is
#![feature(extend_one)]
.About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also uses as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Steps
Unresolved Questions
Extend
is in the prelude, so the explicitextend_
prefix avoids ambiguity.Extend
the best place for just methods?Iterator::unzip
, which only hasDefault + Extend
, so there's not really much choice.Implementation history
The text was updated successfully, but these errors were encountered: