Skip to content
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

Using ByteVec consuming trait methods on BString fails with "cannot move out of dereference of BString" #98

Closed
Byron opened this issue Sep 17, 2021 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@Byron
Copy link
Contributor

Byron commented Sep 17, 2021

My actual goal is to convert a BString into a String, in a way that errors if it's not UTF-8. Basically String::from_utf8(my_bstring.into_inner()).

However, when trying it, I see 'cannot move out of dereference of BString', which makes sense as all functionality is implemented via Deref.

The problem is that my API uses BStr and sometimes BString so it would be desirable to be able dissolve the BString once again.

Here is the playground link.

Thanks for your advice.

@BurntSushi
Copy link
Owner

In terms of how to make progress here, I think you want Vec::from(s).into_string(): https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7492b80f15720c0b4585e9cc74d2359a

But this is probably a specific example of a general problem. Namely, that BString cannot call methods on ByteVec. I wonder how we might accomplish that. I can think of two ways:

  1. Implement ByteVec for BString explicitly.
  2. Add inherent methods on BString for all methods on ByteVec that move self.

I know (2) would work, but would be clunky/unfortunate. I'm less sure about (1), since I wonder how trait method resolution would work for methods that take &self or &mut self. But perhaps everything "just works" in that case.

@Byron Byron changed the title How to get the bytes out of BString? Using ByteVec trait methods on BString fails with "cannot move out of dereference of BString" Sep 17, 2021
@Byron Byron changed the title Using ByteVec trait methods on BString fails with "cannot move out of dereference of BString" Using ByteVec consuming trait methods on BString fails with "cannot move out of dereference of BString" Sep 17, 2021
@Byron
Copy link
Contributor Author

Byron commented Sep 17, 2021

Thanks for the analysis and for showing a way forward.

I know (2) would work, but would be clunky/unfortunate. I'm less sure about (1), since I wonder how trait method resolution would work for methods that take &self or &mut self.

  1. Seems like the most natural way even though it will require quite some repetition/call-forwarding. If there is ambiguity when &self and &mut self are involved, I wonder if it would even compile.

On the flip side, fixing this probably makes the API much more ergonomic, for instance, on BString something like as_bstr() won't work, but as_ref() does, which can be confusing to some me.

@BurntSushi
Copy link
Owner

It turns out that this is a duplicate of #84. Similar ideas on how to move forward are discussed there.

To be honest, I'm still not quite sure what to do. If impl ByteVec for BString makes everything work, then that is definitely the easiest path. But someone has to try it out and see what, if any, failure modes it has.

Anyway, closing in favor of #84 since it was created first and also has a fair bit of context/info there.

@BurntSushi BurntSushi added the duplicate This issue or pull request already exists label Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants