Skip to content

Please use Cow<'static, str> for configuration, rather than String. #545

@maypop-the-dragon

Description

@maypop-the-dragon

Please use Cow<'static, str> for configuration, rather than String.

This way, we will not have to allocate new Strings all the time even when the strings are always the same.
This will also make it possible to construct configurations in const contexts.

This will allow for cleaner code and slightly improved performance.

How it is now:

// This is a config that we will use repeatedly throughout the program.
// It must be inside the body of a function because allocating `String`s is not `const`.
let config = PrettyConfig::new()
    .indentor("\t".to_string())
    .struct_names(true);

// These all involve allocating new `String`s for the new line, indentor, and separator.
println!("{}", to_string_pretty(struct_1, config.clone()).unwrap());
println!("{}", to_string_pretty(struct_2, config.clone()).unwrap());
println!("{}", to_string_pretty(struct_3, config.clone()).unwrap());

How I imagine it could be:

// This can now be global if we want!
const CONFIG: PrettyConfig = PrettyConfig::new()
    .indentor(Cow::Borrowed("\t"))
    .struct_names(true);

// This is now cheaper and cleaner.
println!("{}", to_string_pretty(struct_1, CONFIG).unwrap());
println!("{}", to_string_pretty(struct_2, CONFIG).unwrap());
println!("{}", to_string_pretty(struct_3, CONFIG).unwrap());

P.S. This is my first GitHub issue so sorry if it's, like, bad somehow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions