#![warn(missing_docs)]
#[macro_use]
extern crate nix;
use std::os::raw::c_void;
use wayland_sys::common as syscom;
pub mod filter;
pub mod map;
pub mod socket;
pub mod user_data;
pub mod wire;
pub use smallvec::smallvec;
pub trait MessageGroup: Sized {
const MESSAGES: &'static [wire::MessageDesc];
type Map;
fn opcode(&self) -> u16;
fn is_destructor(&self) -> bool;
fn since(&self) -> u32;
fn child<Meta: self::map::ObjectMetadata>(
opcode: u16,
version: u32,
meta: &Meta,
) -> Option<crate::map::Object<Meta>>;
fn from_raw(msg: wire::Message, map: &mut Self::Map) -> Result<Self, ()>;
fn into_raw(self, send_id: u32) -> wire::Message;
unsafe fn from_raw_c(obj: *mut c_void, opcode: u32, args: *const syscom::wl_argument)
-> Result<Self, ()>;
fn as_raw_c_in<F, T>(self, f: F) -> T
where
F: FnOnce(u32, &mut [syscom::wl_argument]) -> T;
}
pub trait Interface: 'static {
type Request: MessageGroup + 'static;
type Event: MessageGroup + 'static;
const NAME: &'static str;
const VERSION: u32;
fn c_interface() -> *const syscom::wl_interface;
}
pub enum NoMessage {}
#[cfg_attr(tarpaulin, skip)]
impl MessageGroup for NoMessage {
const MESSAGES: &'static [wire::MessageDesc] = &[];
type Map = ();
fn is_destructor(&self) -> bool {
match *self {}
}
fn opcode(&self) -> u16 {
match *self {}
}
fn since(&self) -> u32 {
match *self {}
}
fn child<M: self::map::ObjectMetadata>(_: u16, _: u32, _: &M) -> Option<crate::map::Object<M>> {
None
}
fn from_raw(_: wire::Message, _: &mut ()) -> Result<Self, ()> {
Err(())
}
fn into_raw(self, _: u32) -> wire::Message {
match self {}
}
unsafe fn from_raw_c(
_obj: *mut c_void,
_opcode: u32,
_args: *const syscom::wl_argument,
) -> Result<Self, ()> {
Err(())
}
fn as_raw_c_in<F, T>(self, _f: F) -> T
where
F: FnOnce(u32, &mut [syscom::wl_argument]) -> T,
{
match self {}
}
}
pub struct ThreadGuard<T: ?Sized> {
thread: std::thread::ThreadId,
val: std::mem::ManuallyDrop<T>,
}
impl<T> ThreadGuard<T> {
pub fn new(val: T) -> ThreadGuard<T> {
ThreadGuard {
val: std::mem::ManuallyDrop::new(val),
thread: std::thread::current().id(),
}
}
}
impl<T: ?Sized> ThreadGuard<T> {
pub fn get(&self) -> &T {
self.try_get()
.expect("Attempted to access a ThreadGuard contents from the wrong thread.")
}
pub fn get_mut(&mut self) -> &mut T {
self.try_get_mut()
.expect("Attempted to access a ThreadGuard contents from the wrong thread.")
}
pub fn try_get(&self) -> Option<&T> {
if self.thread == ::std::thread::current().id() {
Some(&self.val)
} else {
None
}
}
pub fn try_get_mut(&mut self) -> Option<&mut T> {
if self.thread == ::std::thread::current().id() {
Some(&mut self.val)
} else {
None
}
}
}
impl<T: ?Sized> Drop for ThreadGuard<T> {
fn drop(&mut self) {
if self.thread == ::std::thread::current().id() {
unsafe { std::mem::ManuallyDrop::drop(&mut self.val) }
}
}
}
unsafe impl<T: ?Sized> Send for ThreadGuard<T> {}
unsafe impl<T: ?Sized> Sync for ThreadGuard<T> {}