-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cxx-qt: add a CxxQtSignalHandler struct and closure trait
This allows for FnMut to be used for signals later. Related to #595
- Loading branch information
1 parent
c408466
commit 25a71a4
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com> | ||
// SPDX-FileContributor: Leon Matthes <leon.matthes@kdab.com> | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use cxx::ExternType; | ||
|
||
/// A trait which describes the closure to be used with [CxxQtSignalHandler]. | ||
pub trait CxxQtSignalHandlerClosure { | ||
/// The Id of the CXX type | ||
type Id; | ||
/// The type of the closure | ||
type FnType: ?Sized; | ||
} | ||
|
||
/// A signal handler helper which is used to move a FnMut closure into C++ | ||
#[repr(transparent)] | ||
pub struct CxxQtSignalHandler<T> | ||
where | ||
T: CxxQtSignalHandlerClosure, | ||
{ | ||
closure: Box<T::FnType>, | ||
} | ||
|
||
impl<T> CxxQtSignalHandler<T> | ||
where | ||
T: CxxQtSignalHandlerClosure, | ||
{ | ||
/// Create a new signal handler with the given closure | ||
// | ||
// Note that we cannot use From as we cannot infer the type in the caller | ||
pub fn new(closure: Box<T::FnType>) -> Self { | ||
Self { closure } | ||
} | ||
|
||
/// A mutable reference to the inner closure | ||
pub fn closure(&mut self) -> &mut Box<T::FnType> { | ||
&mut self.closure | ||
} | ||
} | ||
|
||
// Safety: | ||
// | ||
// Static checks on the C++ and Rust side to ensure the size is the same. | ||
unsafe impl<T> ExternType for CxxQtSignalHandler<T> | ||
where | ||
T: CxxQtSignalHandlerClosure, | ||
{ | ||
type Kind = cxx::kind::Trivial; | ||
type Id = T::Id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters