1#![allow(missing_docs)]
2
3use std::{io, mem, ptr};
4use crate::sys::input_event;
5#[cfg(feature = "bytes")]
6use bytes::{BytesMut, BufMut};
7use crate::InputEvent;
8
9pub struct EventCodec {
10 _dummy: (),
11}
12
13impl EventCodec {
14 pub const fn new() -> Self {
15 EventCodec {
16 _dummy: (),
17 }
18 }
19
20 #[cfg(feature = "bytes")]
21 #[cfg_attr(feature = "dox", doc(cfg(feature = "bytes")))]
22 pub fn decode_bytes(&mut self, src: &mut BytesMut) -> Result<Option<InputEvent>, io::Error> {
23 if src.len() >= mem::size_of::<InputEvent>() {
24 let src = src.split_to(mem::size_of::<InputEvent>());
25 let event = unsafe {
26 let mut event = mem::MaybeUninit::<input_event>::uninit();
27 ptr::copy_nonoverlapping(src.as_ptr(), event.as_mut_ptr() as *mut u8, mem::size_of::<InputEvent>());
28 InputEvent::from_raw(&*event.as_ptr()).map(|e| *e).map_err(From::from)
29 };
30
31 event.map(Some)
32 } else {
33 Ok(None)
34 }
35 }
36
37 #[cfg(feature = "bytes")]
38 #[cfg_attr(feature = "dox", doc(cfg(feature = "bytes")))]
39 pub fn encode_bytes(&mut self, item: InputEvent, dst: &mut BytesMut) -> Result<(), io::Error> {
40 dst.reserve(mem::size_of::<InputEvent>());
41 dst.put_slice(item.as_bytes());
42 Ok(())
43 }
44}
45
46#[cfg(feature = "tokio-util-0_6")]
47#[cfg_attr(feature = "dox", doc(cfg(feature = "tokio-util-0_6")))]
48mod tokio_util_impl_0_6 {
49 use tokio_util_0_6::codec::{Encoder, Decoder};
50 include!("tokio_impl.rs");
51}
52
53#[cfg(feature = "tokio-util-0_7")]
54#[cfg_attr(feature = "dox", doc(cfg(feature = "tokio-util-0_7")))]
55mod tokio_util_impl_0_7 {
56 use tokio_util_0_7::codec::{Encoder, Decoder};
57 include!("tokio_impl.rs");
58}