Comments (12)
This SO post predates the Entry API, which is exactly the solution to their problem, as far as I know.
Can you post some modern code that is causing you trouble?
from collect-rs.
I am indeed using the Entry API and running into the same problem: https://github.com/andrewrk/genesis/blob/18e9b1db57de8c3c5dd06904c482d274a7048240/src/text.rs#L123
from collect-rs.
So what's wrong here? To insert an element you need mutable rights. The fact that it returns a mutable ref that you have to coerce to immutable seems minimal.
from collect-rs.
It ends up using the lifetime from &'a mut self
instead of an immutable lifetime. So the cache value that gets returned is a mutable ref when I wanted an immutable ref. This causes a compile error when you try to use another field from the struct that contains the hash map.
from collect-rs.
Sounds like a bug in Rust, but maybe this is fundamental.
from collect-rs.
@pcwalton is this intended behaviour? &*(&mut T)
prevents future immutable borrows of T?
from collect-rs.
@gankro FWIW it's always been the case.
from collect-rs.
My argument was that in general borrows are not tagged by mutability. Look at this:
fn borrow_thing<'a>(&'a mut self) -> Response<'a> { ... }
If Response<'a>
would be a &'a T
instead, you would think it's illogical. We really can't tell if this is a "true" mutable borrow or not. But in general borrows keep track of what they are derived from, right, not what they produce.
from collect-rs.
Ah okay I talked it out with @nikomatsakis on IRC and there's literally nothing that can be done here with Rust's current type system. Evidently the borrow system does not look at return types to decide the kind of borrow. If you use the lifetime 'a
from an &'a mut
, it will always behave as a mutable borrow regardless of what the 'a is stored in.
That is, fn foo<'a>(&'a mut self) -> &'a Foo
will have &'a Foo behave as an &'a mut Foo
for the purposes of borrow checking. I do not believe that this is a fundamental problem with Rust. The alternative would simply require spidering the return type of a function to "infer" the mutability of the borrow. Currently borrow checking doesn't need to look at the contents of the return type beyond what lifetimes it contains. And indeed it would be harder for a reader of the API to determine the mutability of the borrow in general if this was done. e.g. today if we see fn foo<'a>(&'a mut self) -> Foo<'a>
we know the Foo mutably borrows self, but the proposed system would require a consumer to know the internals of Foo
to know the mutability of the borrow. Since we encourage private-by-default, this may not be trivial to find out.
from collect-rs.
I'm closing this as cant-fix. This is fundamental to the current language design. No API change can handle this if the method needs to take &mut self.
from collect-rs.
Thanks for looking into it.
from collect-rs.
@andrewrk As a hack, you can potentially convert the &mut to a raw ptr, convert it back to an &'static, and then copy_lifetime it with &my_map to make it into a proper shared reference to the insides of the my_map. But this can't be returned from a method that takes &mut my_map, or else it will just get re-upgraded to an owned borrow.
from collect-rs.
Related Issues (20)
- Try out some collection traits HOT 4
- Doc tests not running HOT 3
- Implement `into_iter` method for all collections
- Enhance `LinkedHashMap` HOT 3
- Add Haskell-style traversables HOT 3
- Document trait impl behavior HOT 2
- Compilation fails on rust master
- Clean up and enhance `TreeMap` HOT 7
- Disband collect-rs in favour of separate crates? HOT 33
- `TreeMap::insert`'s behavior differs from `{BTreeMap, HashMap}` HOT 5
- Deprecate `trie` in favor of external crate
- Push new version to crates.io HOT 1
- immut_slist -> cons-list?
- collect does not compile on nightly 3-21 HOT 2
- version was not bumped on crates.io? HOT 4
- Check out cool no-unsafe-code intrusive-style dlist HOT 3
- Tone down some greedy benches? HOT 1
- Some types are missing `Debug` impls HOT 3
- Be consistent with `Keys` and `Values` iterators HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from collect-rs.