-
Notifications
You must be signed in to change notification settings - Fork 7
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
Issue with iterator and lifetimes #15
Comments
Haven't figured out how to express the lifetime restrictions of key & value in Rust. Is // Keep the blocks loaded by the iterator pinned in memory as long as the
// iterator is not deleted, If used when reading from tables created with
// BlockBasedTableOptions::use_delta_encoding = false,
// Iterator's property "rocksdb.iterator.is-key-pinned" is guaranteed to
// return 1.
// Default: false
bool pin_data; |
It seems to me that key() and value() ought to be simply like:
The references remain valid until the iterator is modified by calling
I think the problem here is that iterator doesn't allow you to return references that only valid until .next() is called. |
The
When Haven't figure out how to express the lifetime restriction in Rust. 🤣 |
Workaround: In While collecting for later use: // `it` here, for later use, you must hold the original `mut it` somewhere.
it.map(|(key, val)| (key.to_vec(), val.to_vec())).collect::<Vec<_>>(); |
In my case, that would load the whole multi-gb datastore into memory, not a good plan. A better workaround is to use a while loop with is_valid() |
I will add a link to this issue in README. |
Consider the implementation of next() for Iterator
The self.next() line invalidates the returned references from self.key() and self.value(). Thus this iterator is always returning bad references.
But why didn't Rust catch this?
key (and value) claims to return a reference with lifetime 'a. But this isn't really accurate, as it should only be valid until the next &mut call on self (probably on .next()). Rust doesn't catch the problem because of the unsafe code used to implement the function (obviously unavoidable.)
The text was updated successfully, but these errors were encountered: