| 1 | //! Implement RequestConnection for wrappers around other connection types, e.g. |
| 2 | //! `Box<Connection>`. |
| 3 | |
| 4 | use std::io::IoSlice; |
| 5 | |
| 6 | use x11rb_protocol::x11_utils::{ReplyFDsRequest, ReplyRequest, VoidRequest}; |
| 7 | use x11rb_protocol::{DiscardMode, RawEventAndSeqNumber, SequenceNumber}; |
| 8 | |
| 9 | use crate::connection::{ |
| 10 | BufWithFds, Connection, EventAndSeqNumber, ReplyOrError, RequestConnection, RequestKind, |
| 11 | }; |
| 12 | use crate::cookie::{Cookie, CookieWithFds, VoidCookie}; |
| 13 | use crate::errors::{ConnectionError, ParseError, ReplyError, ReplyOrIdError}; |
| 14 | use crate::protocol::xproto::Setup; |
| 15 | use crate::protocol::Event; |
| 16 | use crate::utils::RawFdContainer; |
| 17 | use crate::x11_utils::{ExtensionInformation, TryParse, TryParseFd, X11Error}; |
| 18 | |
| 19 | macro_rules! impl_deref_request_connection_inner { |
| 20 | () => { |
| 21 | type Buf = C::Buf; |
| 22 | |
| 23 | fn send_request_with_reply<R>( |
| 24 | &self, |
| 25 | bufs: &[IoSlice<'_>], |
| 26 | fds: Vec<RawFdContainer>, |
| 27 | ) -> Result<Cookie<'_, Self, R>, ConnectionError> |
| 28 | where |
| 29 | R: TryParse, |
| 30 | { |
| 31 | (**self) |
| 32 | .send_request_with_reply(bufs, fds) |
| 33 | .map(|cookie| cookie.replace_connection(self)) |
| 34 | } |
| 35 | |
| 36 | fn send_trait_request_with_reply<R>( |
| 37 | &self, |
| 38 | request: R, |
| 39 | ) -> Result<Cookie<'_, Self, <R as ReplyRequest>::Reply>, ConnectionError> |
| 40 | where |
| 41 | R: ReplyRequest, |
| 42 | { |
| 43 | (**self) |
| 44 | .send_trait_request_with_reply(request) |
| 45 | .map(|cookie| cookie.replace_connection(self)) |
| 46 | } |
| 47 | |
| 48 | fn send_request_with_reply_with_fds<R>( |
| 49 | &self, |
| 50 | bufs: &[IoSlice<'_>], |
| 51 | fds: Vec<RawFdContainer>, |
| 52 | ) -> Result<CookieWithFds<'_, Self, R>, ConnectionError> |
| 53 | where |
| 54 | R: TryParseFd, |
| 55 | { |
| 56 | (**self) |
| 57 | .send_request_with_reply_with_fds(bufs, fds) |
| 58 | .map(|cookie| cookie.replace_connection(self)) |
| 59 | } |
| 60 | |
| 61 | fn send_trait_request_with_reply_with_fds<R>( |
| 62 | &self, |
| 63 | request: R, |
| 64 | ) -> Result<CookieWithFds<'_, Self, R::Reply>, ConnectionError> |
| 65 | where |
| 66 | R: ReplyFDsRequest, |
| 67 | { |
| 68 | (**self) |
| 69 | .send_trait_request_with_reply_with_fds(request) |
| 70 | .map(|cookie| cookie.replace_connection(self)) |
| 71 | } |
| 72 | |
| 73 | fn send_request_without_reply( |
| 74 | &self, |
| 75 | bufs: &[IoSlice<'_>], |
| 76 | fds: Vec<RawFdContainer>, |
| 77 | ) -> Result<VoidCookie<'_, Self>, ConnectionError> { |
| 78 | (**self) |
| 79 | .send_request_without_reply(bufs, fds) |
| 80 | .map(|cookie| cookie.replace_connection(self)) |
| 81 | } |
| 82 | |
| 83 | fn send_trait_request_without_reply<R>( |
| 84 | &self, |
| 85 | request: R, |
| 86 | ) -> Result<VoidCookie<'_, Self>, ConnectionError> |
| 87 | where |
| 88 | R: VoidRequest, |
| 89 | { |
| 90 | (**self) |
| 91 | .send_trait_request_without_reply(request) |
| 92 | .map(|cookie| cookie.replace_connection(self)) |
| 93 | } |
| 94 | |
| 95 | fn discard_reply(&self, sequence: SequenceNumber, kind: RequestKind, mode: DiscardMode) { |
| 96 | (**self).discard_reply(sequence, kind, mode) |
| 97 | } |
| 98 | |
| 99 | fn prefetch_extension_information( |
| 100 | &self, |
| 101 | extension_name: &'static str, |
| 102 | ) -> Result<(), ConnectionError> { |
| 103 | (**self).prefetch_extension_information(extension_name) |
| 104 | } |
| 105 | |
| 106 | fn extension_information( |
| 107 | &self, |
| 108 | extension_name: &'static str, |
| 109 | ) -> Result<Option<ExtensionInformation>, ConnectionError> { |
| 110 | (**self).extension_information(extension_name) |
| 111 | } |
| 112 | |
| 113 | fn wait_for_reply_or_error( |
| 114 | &self, |
| 115 | sequence: SequenceNumber, |
| 116 | ) -> Result<Self::Buf, ReplyError> { |
| 117 | (**self).wait_for_reply_or_error(sequence) |
| 118 | } |
| 119 | |
| 120 | fn wait_for_reply_or_raw_error( |
| 121 | &self, |
| 122 | sequence: SequenceNumber, |
| 123 | ) -> Result<ReplyOrError<Self::Buf>, ConnectionError> { |
| 124 | (**self).wait_for_reply_or_raw_error(sequence) |
| 125 | } |
| 126 | |
| 127 | fn wait_for_reply( |
| 128 | &self, |
| 129 | sequence: SequenceNumber, |
| 130 | ) -> Result<Option<Self::Buf>, ConnectionError> { |
| 131 | (**self).wait_for_reply(sequence) |
| 132 | } |
| 133 | |
| 134 | fn wait_for_reply_with_fds( |
| 135 | &self, |
| 136 | sequence: SequenceNumber, |
| 137 | ) -> Result<BufWithFds<Self::Buf>, ReplyError> { |
| 138 | (**self).wait_for_reply_with_fds(sequence) |
| 139 | } |
| 140 | |
| 141 | fn wait_for_reply_with_fds_raw( |
| 142 | &self, |
| 143 | sequence: SequenceNumber, |
| 144 | ) -> Result<ReplyOrError<BufWithFds<Self::Buf>, Self::Buf>, ConnectionError> { |
| 145 | (**self).wait_for_reply_with_fds_raw(sequence) |
| 146 | } |
| 147 | |
| 148 | fn check_for_error(&self, sequence: SequenceNumber) -> Result<(), ReplyError> { |
| 149 | (**self).check_for_error(sequence) |
| 150 | } |
| 151 | |
| 152 | fn check_for_raw_error( |
| 153 | &self, |
| 154 | sequence: SequenceNumber, |
| 155 | ) -> Result<Option<Self::Buf>, ConnectionError> { |
| 156 | (**self).check_for_raw_error(sequence) |
| 157 | } |
| 158 | |
| 159 | fn prefetch_maximum_request_bytes(&self) { |
| 160 | (**self).prefetch_maximum_request_bytes() |
| 161 | } |
| 162 | |
| 163 | fn maximum_request_bytes(&self) -> usize { |
| 164 | (**self).maximum_request_bytes() |
| 165 | } |
| 166 | |
| 167 | fn parse_error(&self, error: &[u8]) -> Result<X11Error, ParseError> { |
| 168 | (**self).parse_error(error) |
| 169 | } |
| 170 | |
| 171 | fn parse_event(&self, event: &[u8]) -> Result<Event, ParseError> { |
| 172 | (**self).parse_event(event) |
| 173 | } |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | macro_rules! impl_deref_connection_inner { |
| 178 | () => { |
| 179 | fn wait_for_event(&self) -> Result<Event, ConnectionError> { |
| 180 | (**self).wait_for_event() |
| 181 | } |
| 182 | |
| 183 | fn wait_for_raw_event(&self) -> Result<Self::Buf, ConnectionError> { |
| 184 | (**self).wait_for_raw_event() |
| 185 | } |
| 186 | |
| 187 | fn wait_for_event_with_sequence(&self) -> Result<EventAndSeqNumber, ConnectionError> { |
| 188 | (**self).wait_for_event_with_sequence() |
| 189 | } |
| 190 | |
| 191 | fn wait_for_raw_event_with_sequence( |
| 192 | &self, |
| 193 | ) -> Result<RawEventAndSeqNumber<Self::Buf>, ConnectionError> { |
| 194 | (**self).wait_for_raw_event_with_sequence() |
| 195 | } |
| 196 | |
| 197 | fn poll_for_event(&self) -> Result<Option<Event>, ConnectionError> { |
| 198 | (**self).poll_for_event() |
| 199 | } |
| 200 | |
| 201 | fn poll_for_raw_event(&self) -> Result<Option<Self::Buf>, ConnectionError> { |
| 202 | (**self).poll_for_raw_event() |
| 203 | } |
| 204 | |
| 205 | fn poll_for_event_with_sequence( |
| 206 | &self, |
| 207 | ) -> Result<Option<EventAndSeqNumber>, ConnectionError> { |
| 208 | (**self).poll_for_event_with_sequence() |
| 209 | } |
| 210 | |
| 211 | fn poll_for_raw_event_with_sequence( |
| 212 | &self, |
| 213 | ) -> Result<Option<RawEventAndSeqNumber<Self::Buf>>, ConnectionError> { |
| 214 | (**self).poll_for_raw_event_with_sequence() |
| 215 | } |
| 216 | |
| 217 | fn flush(&self) -> Result<(), ConnectionError> { |
| 218 | (**self).flush() |
| 219 | } |
| 220 | |
| 221 | fn setup(&self) -> &Setup { |
| 222 | (**self).setup() |
| 223 | } |
| 224 | |
| 225 | fn generate_id(&self) -> Result<u32, ReplyOrIdError> { |
| 226 | (**self).generate_id() |
| 227 | } |
| 228 | }; |
| 229 | } |
| 230 | |
| 231 | macro_rules! impl_deref_connection { |
| 232 | ($type:ty) => { |
| 233 | impl<C: RequestConnection + ?Sized> RequestConnection for $type { |
| 234 | impl_deref_request_connection_inner!(); |
| 235 | } |
| 236 | impl<C: Connection + ?Sized> Connection for $type { |
| 237 | impl_deref_connection_inner!(); |
| 238 | } |
| 239 | }; |
| 240 | } |
| 241 | |
| 242 | impl_deref_connection!(&C); |
| 243 | impl_deref_connection!(&mut C); |
| 244 | impl_deref_connection!(Box<C>); |
| 245 | impl_deref_connection!(std::sync::Arc<C>); |
| 246 | impl_deref_connection!(std::rc::Rc<C>); |
| 247 | |
| 248 | impl<C: RequestConnection + ToOwned + ?Sized> RequestConnection for std::borrow::Cow<'_, C> { |
| 249 | impl_deref_request_connection_inner!(); |
| 250 | } |
| 251 | impl<C: Connection + ToOwned + ?Sized> Connection for std::borrow::Cow<'_, C> { |
| 252 | impl_deref_connection_inner!(); |
| 253 | } |
| 254 | |