[pbs-devel] [PATCH proxmox] schema: add const fn unwrap_*_schema/format methods

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Feb 23 09:31:03 CET 2022


On 22.02.22 09:47, Wolfgang Bumiller wrote:
> 'unwrap_' because they will panic and as `const fn` since
> panic in const fn is now possible
> 
> Note that const evaluation will only be triggered when
> actually used in const context, so to ensure *compile time*
> checks, use something like this:
> 
>     const FOO_SCHEMA: &AllOfSchema =
>         SomeType::API_SCHEMA.unwrap_all_of_schema();
>     then_use(FOO_SCHEMA);
> 
> or to use the list of enum values of an enum string type
> with compile time checks:
> 
>     const LIST: &'static [EnumEntry] =
>         AnEnumStringType::API_SCHEMA
>             .unwrap_string_schema()
>             .unwrap_format()
>             .unwrap_enum_format();
>     for values in LIST {
>         ...
>     }
> 
> Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
> ---
> While schemas are usually unlikely to change type or lose properties
> such as enum lists, for `ObjectSchema` and `AllOfSchema` this may
> actually allow catching future issues at build-time...
> 
> If we want to do this, I'd prepare a similar patch set for all the
> `ApiHandler::...` value extractions we do in the CLI in pbs (just look
> at the output of `egrep -B1 -nr 'unreachable' ./src ./*/src` in pbs ;-) )
> 

IMO the usage gets nicer, lots of removal of unreachable!(), and even if
not used correctly (without const) we're as good as now; so can only win.

If there's no objection from others I'd say: go for it.





More information about the pbs-devel mailing list