Ho creato una struttura dati in Rust e voglio creare iteratori per questo. Gli iteratori immutabili sono abbastanza facili. Al momento ho questo, e funziona bene:Come posso creare la mia struttura dati con un iteratore che restituisce riferimenti mutabili?
// This is a mock of the "real" EdgeIndexes class as
// the one in my real program is somewhat complex, but
// of identical type
struct EdgeIndexes;
impl Iterator for EdgeIndexes {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
Some(0)
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}
pub struct CGraph<E> {
nodes: usize,
edges: Vec<E>,
}
pub struct Edges<'a, E: 'a> {
index: EdgeIndexes,
graph: &'a CGraph<E>,
}
impl<'a, E> Iterator for Edges<'a, E> {
type Item = &'a E;
fn next(&mut self) -> Option<Self::Item> {
match self.index.next() {
None => None,
Some(x) => Some(&self.graph.edges[x]),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.index.size_hint()
}
}
Voglio creare un iteratore che restituisce riferimenti mutabili pure. Ho provato a fare questo, ma non riesco a trovare un modo per farlo compilare:
pub struct MutEdges<'a, E: 'a> {
index: EdgeIndexes,
graph: &'a mut CGraph<E>
}
impl<'a, E> Iterator<&'a mut E> for MutEdges<'a, E> {
fn next(&mut self) -> Option<&'a mut E> {
match (self.index.next()) {
None => None,
Some(x) => Some(self.graph.edges.get_mut(x))
}
}
fn size_hint(&self) -> (uint, Option<uint>) {
self.index.size_hint()
}
}
Compilazione questo si traduce nel seguente errore:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/main.rs:53:24
|
53 | Some(x) => self.graph.edges.get_mut(x),
| ^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 50:45...
--> src/main.rs:50:46
|
50 | fn next(&mut self) -> Option<Self::Item> {
| ______________________________________________^
51 | | match self.index.next() {
52 | | None => None,
53 | | Some(x) => self.graph.edges.get_mut(x),
54 | | }
55 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:53:24
|
53 | Some(x) => self.graph.edges.get_mut(x),
| ^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the body at 50:45...
--> src/main.rs:50:46
|
50 | fn next(&mut self) -> Option<Self::Item> {
| ______________________________________________^
51 | | match self.index.next() {
52 | | None => None,
53 | | Some(x) => self.graph.edges.get_mut(x),
54 | | }
55 | | }
| |_____^
note: ...so that types are compatible (expected std::iter::Iterator, found std::iter::Iterator)
--> src/main.rs:50:46
|
50 | fn next(&mut self) -> Option<Self::Item> {
| ______________________________________________^
51 | | match self.index.next() {
52 | | None => None,
53 | | Some(x) => self.graph.edges.get_mut(x),
54 | | }
55 | | }
| |_____^
io sono sicuro come interpretare questi errori e come cambiare il mio codice per consentire MutEdges
di restituire riferimenti mutabili.
Collegamento a playground with code.
Non sono sicuro, ma potrebbe essere lo stesso problema di http://stackoverflow.com/questions/25702909/can-i-write-an-iterator-that-yields-a-reference-into -appunto – Levans
Non proprio, penso. Il mio iteratore non possiede gli oggetti a cui restituisce riferimenti mutevoli, cosa che fa. Penso che sia possibile dato che la libreria standard di Rust [ha già iteratori di riferimenti mutabili] (http://doc.rust-lang.org/std/slice/struct.MutItems.html) –
La loro implementazione usa la funzione deprecata ' mut_shift_ref() ', forse puoi trovare quello che ti serve lì: http://doc.rust-lang.org/std/slice/trait.MutableSlice.html#tymethod.mut_shift_ref – Levans