Code Monkey home page Code Monkey logo

Comments (5)

eddyb avatar eddyb commented on May 29, 2024

I'd say [u8] should be the main type to talk about here, since Vec<u8> derefs to it.
(But the &[u8] syntax may not have been meant literally, as BStr is a newtype around [u8], not around a reference)

And, yeah, I'd be fine with BStr that derefs to [u8], since I'd prefer having most of the functionality in libcore, and just get e.g. the debug formatting from this crate.

from bstr.

joshtriplett avatar joshtriplett commented on May 29, 2024

from bstr.

BurntSushi avatar BurntSushi commented on May 29, 2024

One little hiccup that I've run into is that a lot of the current BStr APIs accept anything that implements AsRef<[u8]>. So that means you can use somebstr.find("foo") or somebstr.find(b"foo"), which is nice for ergonomics. I can continue to do this for the extension trait APIs of course, but with an extension trait, there are several methods already defined on slices that we wouldn't want to redefine. For example, there are already starts_with and ends_with methods defined on &[u8], but their only valid parameter is &[u8] which means somebstr.starts_with("foo") won't work. I see a few choices here, all of which kind of suck honestly:

  1. Don't define methods like starts_with that already exist and keep the AsRef<[u8]> for APIs we do define. We live with the inconsistency. (Users will invariably ask, "why do some methods accept a &str but others don't?!")
  2. When there are conflicting methods, like starts_with, provide "str" focused variants, e.g., starts_with_str and ends_with_str.
  3. When there are conflicting methods, define new methods with the same name and require users to disambiguate methods via UFCS. (Yuck. Only listed for completeness.)
  4. Get rid of the AsRef<[u8]> convenience on all APIs and make everything accept a concrete &[u8] in order to be consistent. This is kind of a bummer, since then you can't just write, somebstr.find("☃☃☃"), but instead must write somebstr.find("☃☃☃".as_bytes()).

Any thoughts on what the preferred route ought to be?

from bstr.

lespea avatar lespea commented on May 29, 2024

I like the second option personally

from bstr.

BurntSushi avatar BurntSushi commented on May 29, 2024

@lespea Could you elaborate a bit more? :)

from bstr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.