ublox_sockets/ref_.rs
1use core::ops::{Deref, DerefMut};
2
3/// A smart pointer to a socket.
4///
5/// Allows the network stack to efficiently determine if the socket state was changed in any way.
6pub struct Ref<'a, T: 'a> {
7 socket: &'a mut T,
8 consumed: bool,
9}
10
11impl<'a, T: 'a> Ref<'a, T> {
12 /// Wrap a pointer to a socket to make a smart pointer.
13 ///
14 /// Calling this function is only necessary if your code is using [into_inner].
15 ///
16 /// [into_inner]: #method.into_inner
17 pub fn new(socket: &'a mut T) -> Self {
18 Ref {
19 socket,
20 consumed: false,
21 }
22 }
23
24 /// Unwrap a smart pointer to a socket.
25 ///
26 /// The finalization code is not run. Prompt operation of the network stack depends
27 /// on wrapping the returned pointer back and dropping it.
28 ///
29 /// Calling this function is only necessary to achieve composability if you *must*
30 /// map a `&mut SocketRef<'a, XSocket>` to a `&'a mut XSocket` (note the lifetimes);
31 /// be sure to call [new] afterwards.
32 ///
33 /// [new]: #method.new_unchecked
34 pub fn into_inner(mut ref_: Self) -> &'a mut T {
35 ref_.consumed = true;
36 ref_.socket
37 }
38}
39
40impl<'a, T> Deref for Ref<'a, T> {
41 type Target = T;
42
43 fn deref(&self) -> &Self::Target {
44 self.socket
45 }
46}
47
48impl<'a, T> DerefMut for Ref<'a, T> {
49 fn deref_mut(&mut self) -> &mut Self::Target {
50 self.socket
51 }
52}