domain icon indicating copy to clipboard operation
domain copied to clipboard

Make RelativeName::from_slice const fn

Open WhyNotHugo opened this issue 2 months ago • 1 comments

Allow defining relative names which are validated resolved at compile time. This is mostly useful for infallible functions which need to use a RelativeName which is not taken as input.

This is also useful for traits where each implementation needs to define a specific RelativeName. E.g.:

trait DiscoverableService {
    const RELATIVE_NAME: RelativeName<u8>;
}

Example (also included in test module):

const VALID_NAME: &RelativeName<[u8]> =
    match RelativeName::from_slice(b"\x03www\x07example") {
        Ok(name) => name,
        Err(_) => panic!("VALID_NAME failed at compile time"),
    };

A few other functions in the call tree of from_slice had to be made const fn too, some with small changes (like match incited of ?, or inlining .into() calls for errors).

WhyNotHugo avatar Sep 29 '25 08:09 WhyNotHugo