Skip to content

Commit

Permalink
cxx-qt-lib: Add const_assert for asserting at compile-time
Browse files Browse the repository at this point in the history
This is needed to ensure the Rust-side QMessageLogContext struct has the
expected size.
  • Loading branch information
redstrate committed Jan 15, 2025
1 parent bbb4cde commit 68454a7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/cxx-qt-lib/src/core/qtlogging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ pub struct QMessageLogContext<'a> {
_phantom: PhantomData<&'a c_char>,
}

const_assert!(
size_of::<QMessageLogContext>() == (size_of::<i32>() * 2) + (size_of::<*const c_char>() * 3)
);

impl<'a> QMessageLogContext<'a> {
pub fn new(
file: &'a CStr,
Expand Down Expand Up @@ -125,5 +129,6 @@ unsafe impl ExternType for QMessageLogContext<'_> {
type Kind = cxx::kind::Trivial;
}

use crate::const_assert;
use crate::core::qtlogging::ffi::category;
pub use ffi::{qt_message_output, QtMsgType};
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ pub use crate::qml::*;
mod quickcontrols;
#[cfg(feature = "qt_quickcontrols")]
pub use crate::quickcontrols::*;

mod util;
18 changes: 18 additions & 0 deletions crates/cxx-qt-lib/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// SPDX-FileContributor: Joshua Goins <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

/// Asserts that a boolean expression is true at compile time.
///
/// See [`core::assert!`] for more information.
///
/// ```compile_fail
/// const_assert!(5 == 4);
/// ```
#[macro_export]
macro_rules! const_assert {
($x:expr $(,)?) => {
const _: () = ::core::assert!($x);
};
}

0 comments on commit 68454a7

Please sign in to comment.