The upstream cxx docs state about shared enums:
Pattern matching with match still works but will require you to write wildcard arms to handle the situation of an enum value that is not one of the listed variants.
This is unfortunate as there is no way to check that all listed enum variants are actually handled. IOW this code does not even warn:
#[cxx::bridge]
mod ffi {
enum CxxEnum {
A,
B,
C = 5,
}
}
use ffi::CxxEnum;
match t {
CxxEnum::A => {},
CxxEnum::B => {},
_ => {},
}This generates an extra _CxxEnum! macro that matches all the ranges that are not listed by the enum. When using that macro instead of the _ catch-all, we get compile-time errors if we forget to match variants:
#[cxx_enum_gaps]
#[cxx::bridge]
mod ffi {
enum CxxEnum {
A,
B,
C = 5,
}
}
use ffi::CxxEnum;
match t {
CxxEnum::A => {},
CxxEnum::B => {},
_CxxEnum!() => {},
}
// Error Enum{ repr: 5 } case not coveredThe generated macro looks like:
macro_rules! _CxxEnum {
() => { CxxEnum{ repr: 4..5 | 6.. } };
($i:ident) => { CxxEnum { repr: $i @ (4..5 | 6..) } };
}It can also be used to ergonomically retrieve the inner value in the unlisted case:
match t {
CxxEnum::A => {},
CxxEnum::B => {},
CxxEnum::C => {},
_CxxEnum!(value) => panic!("Unexpected CxxEnum value {value}"),
}This was originally proposed upstream at dtolnay/cxx#1376.
cxx-enum-gaps is Copyright (C) Klarälvdalens Datakonsult AB, and is available under the terms of the MIT or the Apache-2.0 licenses.
The src/syntax subdirectory is copied from cxx and Copyright (C) David Tolnay.
Contact KDAB at info@kdab.com to inquire about additional features or services related to this project.
The KDAB Group is the global No.1 software consultancy for Qt, C++ and OpenGL applications across desktop, embedded and mobile platforms.
The KDAB Group provides consulting and mentoring for developing Qt applications from scratch and in porting from all popular and legacy frameworks to Qt. We continue to help develop parts of Qt and are one of the major contributors to the Qt Project. We can give advanced or standard trainings anywhere around the globe on Qt as well as C++, OpenGL, 3D and more.
Please visit https://www.kdab.com to meet the people who write code like this.