Skip to content

Conversation

@WhyNotHugo
Copy link
Contributor

@WhyNotHugo WhyNotHugo commented Sep 29, 2025

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 WhyNotHugo force-pushed the const-relativename-from_slice branch from 85db9d7 to 049e533 Compare September 29, 2025 08:14
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 WhyNotHugo force-pushed the const-relativename-from_slice branch from 049e533 to 709bdbb Compare September 29, 2025 08:16
@WhyNotHugo
Copy link
Contributor Author

This is also usable for patterns like:

  pub struct DiscoverableService {
      pub relative_domain: &'static RelativeName<[u8]>,
      pub scheme: Scheme,
      pub well_known_path: &'static str,
      pub default_port: u16,
      pub access_field: &'static str,
      pub display_name: &'static str,
  }

  impl DiscoverableService {
      pub const CALDAVS: Self = Self {
          relative_domain: match RelativeName::from_slice(b"\x08_caldavs\x04_tcp") {},
          scheme: Scheme::HTTPS,
          well_known_path: "/.well-known/caldav",
          default_port: 443,
          access_field: "calendar-access",
          display_name: "caldavs",
      };
..

@tertsdiepraam tertsdiepraam requested a review from partim October 30, 2025 16:35
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