Comments (2)
This is a common problem when using traits that are generic in the trait, rather than in the method. Rust's type resolver does not handle ambiguity in intermediate positions of expressions, so if an expression such as item.trait_call()
can denote multiple expansions to multiple types, it will not attempt to choose one.
The problem is that the .as_ref()
calls can refer to both <[T; N] as AsRef<[T]>>::as_ref(&[T; N]) -> &[T]
and <[T; N]> as AsRef<BitSlice<_, T>>::as_ref(&[T; N]) -> &BitSlice<_, T>
.
When working with fixed-size arrays, the coercion from [T; N]
to [T]
can be accomplished through, as you note, .as_{ref,mut}()
without any imports (As{Ref,Mut}
are in the implicit prelude); .borrow_{,mut}()
with an import (std::borrow::Borrow{,Mut}
are not), or, as you note, through range indexing ([..]
). Use of .as_{ref,mut}()
is generally not preferred except when the result of the method call is going directly to a statically-typed slot, precisely because the call is ambiguous.
For what it's worth, you don't actually need to explicitly reborrow when indexing: the expressions (&{,mut} value[..]).method()
and value[..].method()
are equivalent. This also has the advantage of declaring that the value is being interpreted as a slice, rather than creating a view of another type, which is what the As{Ref,Mut}
traits do.
All that said, I've been deliberating whether to remove the As{Ref,Mut}<BitSlice<_, T>>
implementations as I wrote this. As this issue demonstrates, the .as_{ref,mut}()
view production does not allow users to specify the type of the view produced in the method call: it must be done with a fixed-type binding site (let view: &View = expr.as_ref();
) or explicit call (AsRef::<View>>::as_ref(&expr)
. This must be done for production of BitSlice
views anyway, as they have an additional type parameter that cannot be taken from the source expression.
I think the correct thing to do is to remove the As{Ref,Mut}
implementations, since I provide a trait to produce views that does correctly collect the missing type parameter.
Thanks for this issue; the next minor-version release will remove the traits.
from bitvec.
Thanks for the problem fix and such a detailed explanation!
from bitvec.
Related Issues (20)
- `to_bitvec` and related functions returning invalid values HOT 3
- `bitvec` contains unsound global state HOT 4
- `chunks_exact_mut()` occasionally doesn't return the last chunk when `step_by()` is involved
- Difference between `BitVec` and `BitVec<T, O>`? HOT 1
- `copy_within` panics on empty range
- `bitarr!` macro link incorrect
- Implement `BitStore` for signed integer types
- impl `TryFrom<BitVec>` for `BitArray`
- How to convert bitvec to slice? HOT 1
- Build for STM32 (no_std, thumbv7m-none-eabi) - no global memory allocator HOT 1
- Regarding the safety of `chunks_mut(...).remove_alias()` HOT 1
- `BitSlice::shift_start` and `BitSlice::shift_end` argument bounds incorrectly documented
- Incorrect handling of end index in BitVec::extend_from_within
- Implement `Send` for `BitValIter`
- How to "not" an entire `BitVec` in place? HOT 1
- Example of zero-initialized BitVec?
- Cargo check failed with error: could not compile `bitvec` (lib) HOT 2
- crate doesnt compile on arduino HOT 1
- crate tests do not compile with MSRV 1.56 due to dependencies dropping support for that version
- `bits!` macro causes "cannot find type `Lsb0` in this scope"
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 bitvec.