-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
Can’t move out of BString using into
methods
#84
Comments
This is a limitation of But I note that strictly speaking, the title of your issue is wrong. You can of course move out of a use bstr::BString;
fn main() {
let x = BString::from("foo");
let y = <Vec<u8>>::from(x);
println!("{:?}", y);
} |
into
methods
Can you not implement the into methods on BString and forward them to the inner impl BString {
pub fn into_string_lossy(self) -> String {
self.bytes.into_string_lossy()
}
}
OK, I updated the title to not be wrong. |
If you quoted the rest of that paragraph...
To elaborate:
I'm not saying "no," but those are my thoughts currently. |
Ugh, I’m sorry about that. I’m having one of those days. I agree that it is crappy for maintenance to have to reimplement methods (this is one of my own top pain points writing newtypes). Hopefully rust-lang/rfcs#997 eventually eliminates the need to do it, though I don’t see any indication that that’s coming soon. At least implementing forwarding methods is simple enough to do by macro, so boilerplate would be minimal, and the docs on the methods could be similarly rote (“Consumes the inner Vec by forwarding to [`ByteVec::<into_whatever>`].” or something). To your point about user confusion, as a user I would say it is probably more confusing to have a Ergomonically, I feel like it is not great for a consumer to have to e.g. If you really feel like reimplementing the into methods on pub fn into_vec(self) -> Vec<u8> {
self.bytes
} Thanks for replying so quickly, and apologies again for my brain dysfunction. |
Those are all fair points. I suppose if we could do this will minimal fuss using a macro, then I might be on board with it. Adding more explicit constructors and eliminators to the APIs of |
Also stumbled upon this. This is a highly misleading error, I wish we had either a working |
The "better error message" part of that isn't really something |
Thanks for clarifying. How about having a supplement trait extended by ByteVec and implemented for all the types which are currently broken due to deref issue via a macro? Would require some experimenting, but could work? I'm assuming rustc will pick shorter implementation path. If that doesn't work, a macro could address maintainability issues for directly implemented items: macro_rules! intos {
($($mod: tt)?) => {
/// implemeted in [`test`] macro.
$($mod)? fn test() {
println!("hello")
}
};
}
struct BString;
impl BString {
intos!(pub);
}
trait VecExt {
intos!();
}
impl VecExt for Vec<u8> {} |
Because
ByteVec
methods operate through deref onBString
, it is impossible to use the ones with move semantics (into_string
etc.) to move out of aBString
, which is confusing.The text was updated successfully, but these errors were encountered: