GitXplorerGitXplorer
b

overload-strings

public
11 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
cf44f662c88677d5ccf99224c12f4f396b026a48

Should use "plugin=true".

bbirkenfeld committed 8 years ago
Unverified
e1d025ce4ddc499f6ba48ff01f4bc71b94dba720

Add description/repo link.

bbirkenfeld committed 8 years ago
Unverified
3aa65753de8d80a2dc6bd0713c41f5154e73aa88

Add example to readme.

bbirkenfeld committed 8 years ago
Unverified
fe360036199af2525d593fec46ec05621b3ae450

Make example into test.

bbirkenfeld committed 8 years ago
Unverified
215ef5e54f42a013951cce0f46673e013a46bf33

Ignore static/consts.

bbirkenfeld committed 8 years ago
Unverified
953b4644395d0bef23b647546a33c37e0de44d2d

Ignore submodules and double annotation.

bbirkenfeld committed 8 years ago

README

The README file for this repository.

overload-strings

This is a quick and silly syntax extension, mostly to familiarize myself with compiler plugins using custom attributes. It does the equivalent of Haskell's OverloadedStrings, i.e. it inserts an .into() call onto every string literal. No extra trait is necessary.

Usage

As a compiler plugin, requires nightly Rust. Add

#![feature(plugin)]
#![plugin(overload_strings)]

then apply the #[overload_strings] attribute on the item(s) you want to overload string literals in (module, fn, impl, ...).

The annotation does not automatically recurse into submodules, to keep surprises due to nonlocal effects down. It also ignores statics and consts, because they cannot contain method calls.

Where the ambiguity leads to errors in type inference, you can use the type_ascription nightly feature to disambiguate.

Now you can call functions expecting String, Cow<str>, and all other types that implement From<&str> with a string literal:

struct Person {
    first: String,
    last: String,
    birthplace: Cow<str>,
}

process_persons(&[
    Person { first: "Doug", last: "Piranha", birthplace: "London" },
    Person { first: "Dinsdale", last: "Piranha", birthplace: "London" },
]);