Giter Club home page Giter Club logo

astr-rs's People

Contributors

edgarogh avatar lperlaki avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

edgarogh

astr-rs's Issues

Add a way to build an AStr from a `Display` implementation

Context

The primary goal of this crate seems to be to avoid needless allocations for small constant-sized strings. Some of these strings can include IDs, UUIDs, PINs, MAC addresses, RGB hex colors etc. which may have a compact in-memory representation and a less compact string representation whose length is strictly constant (i.e. a UUID will always be 36 bytes long in string form). The "compact" representation often comes with a Debug or Display implementation which is solely responsible for the conversion.

Feature request

I think it would be convenient to have a way to build an AStr from any type that implements Display, given an expected total length. If the Display implementation writes too few or too much characters, an error is returned instead.

Implementation

The additional constructor could look something like AStr::from_fmt(fmt: &dyn std::fmt::Display). An equivalent method for the Debug trait is superfluous as calling format_args!("{something:?}") will return a Displayable Arguments<'_> instance that calls the Debug implementation of "something". With a macro as explained below, you wouldn't even call this function manually most of the time.

I have a working implementation in one of my projects, which also comes with a format_astr!() macro to build AStrs even faster. I would love to open a PR, but if you're interested, I would like guidance on the exact constructor name and signature that you would like, whether a format_astr! macro is in-scope for this PR, and whether or not the macro should take an optional parameter to specify the expected length (or else, type inference would do the job alone).

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.