-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Statically type user data of objects (#38)
- Loading branch information
1 parent
175fae8
commit 070f4ec
Showing
3 changed files
with
72 additions
and
12 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
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
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,51 @@ | ||
use std::{marker::PhantomData, ops::Deref}; | ||
|
||
use smithay_client_toolkit::reexports::client::{Dispatch, Proxy}; | ||
|
||
#[derive(Debug)] | ||
pub struct WlTyped<I, DATA>(I, PhantomData<DATA>); | ||
|
||
impl<I, DATA> WlTyped<I, DATA> | ||
where | ||
I: Proxy, | ||
DATA: Send + Sync + 'static, | ||
{ | ||
#[allow(clippy::extra_unused_type_parameters)] | ||
pub fn wrap<STATE>(i: I) -> Self | ||
where | ||
STATE: Dispatch<I, DATA>, | ||
{ | ||
Self(i, PhantomData) | ||
} | ||
|
||
pub fn inner(&self) -> &I { | ||
&self.0 | ||
} | ||
|
||
pub fn data(&self) -> &DATA { | ||
// Generic on Self::wrap makes sure that this will never panic | ||
#[allow(clippy::unwrap_used)] | ||
self.0.data().unwrap() | ||
} | ||
} | ||
|
||
impl<I: Clone, D> Clone for WlTyped<I, D> { | ||
fn clone(&self) -> Self { | ||
Self(self.0.clone(), PhantomData) | ||
} | ||
} | ||
|
||
impl<I, D> Deref for WlTyped<I, D> { | ||
type Target = I; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<I: PartialEq, D> PartialEq for WlTyped<I, D> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.0 == other.0 | ||
} | ||
} | ||
impl<I: Eq, D> Eq for WlTyped<I, D> {} |