Skip to content

Implement StaticString trait to produce &'static str from symbol types#159

Merged
soareschen merged 17 commits intomainfrom
static-string
Sep 16, 2025
Merged

Implement StaticString trait to produce &'static str from symbol types#159
soareschen merged 17 commits intomainfrom
static-string

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented Sep 16, 2025

Summary

This PR introduces the StaticString trait, which makes it possible to produce a &'static str value from a generic symbol type:

pub trait StaticString {
    const VALUE: &'static str;
}

As an example, the type Symbol!("abc") would implement StaticString with "abc" as VALUE.

Symbol Type

The &'static str value is produced as a const through const evaluation. To support the const creation of strings, the Symbol! macro has been modified to include the byte length of the string inside the expansion. For example, Symbol!("abc") would be expanded into:

Symbol<3, Char<'a', Char<'b', Char<'c', Nil>>>>

The Symbol type is defined as:

pub struct Symbol<const LEN: usize, Chars>(pub PhantomData<Chars>);

which basically attaches the byte length of the string as metadata. This is needed for now, as it is not possible to make use of the calculation of the byte length through const generics evaluation inside a type expression like [u8; T::LEN]. On the other hand, by including the LEN explicitly, we can avoid doing any const evaluation and use that to produce [u8; LEN].

ψ and ζ Aliases

The new Symbol type has ψ as its alias, and the alias for Char has been changed from ι to ζ. So with this, the type Symbol!("abc") is shown as the following inside macro expansion:

ψ<3, ζ<'a', ζ<'b', ζ<'c', ε>>>>

The reason we renamed ι to ζ for Char is because ι will trigger the confusable_idents warning in Rust if the user code contains the use of i as an identifier.

@soareschen soareschen merged commit 1825030 into main Sep 16, 2025
5 checks passed
@soareschen soareschen deleted the static-string branch September 16, 2025 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant