Expose wrapping and unwrapping of SocketRefs.
This commit is contained in:
parent
5c2deb5dd2
commit
103fff9233
|
@ -108,12 +108,10 @@ macro_rules! from_socket {
|
||||||
impl<'a, 'b> AnySocket<'a, 'b> for $socket {
|
impl<'a, 'b> AnySocket<'a, 'b> for $socket {
|
||||||
fn downcast<'c>(ref_: SocketRef<'c, Socket<'a, 'b>>) ->
|
fn downcast<'c>(ref_: SocketRef<'c, Socket<'a, 'b>>) ->
|
||||||
Option<SocketRef<'c, Self>> {
|
Option<SocketRef<'c, Self>> {
|
||||||
SocketRef::map(ref_, |socket| {
|
match SocketRef::unwrap(ref_) {
|
||||||
match *socket {
|
&mut Socket::$variant(ref mut socket) => Some(SocketRef::wrap(socket)),
|
||||||
Socket::$variant(ref mut socket) => Some(socket),
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,21 +32,29 @@ pub struct Ref<'a, T: Session + 'a> {
|
||||||
consumed: bool,
|
consumed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: Session> Ref<'a, T> {
|
impl<'a, T: Session + 'a> Ref<'a, T> {
|
||||||
pub(crate) fn new(socket: &'a mut T) -> Self {
|
/// Wrap a pointer to a socket to make a smart pointer.
|
||||||
|
///
|
||||||
|
/// Calling this function is only necessary if your code is using [unwrap].
|
||||||
|
///
|
||||||
|
/// [unwrap]: #method.unwrap
|
||||||
|
pub fn wrap(socket: &'a mut T) -> Self {
|
||||||
Ref { socket, consumed: false }
|
Ref { socket, consumed: false }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T: Session + 'a> Ref<'a, T> {
|
/// Unwrap a smart pointer to a socket.
|
||||||
pub(crate) fn map<U, F>(mut ref_: Self, f: F) -> Option<Ref<'a, U>>
|
///
|
||||||
where U: Session + 'a, F: FnOnce(&'a mut T) -> Option<&'a mut U> {
|
/// The finalization code is not run. Prompt operation of the network stack depends
|
||||||
if let Some(socket) = f(ref_.socket) {
|
/// on wrapping the returned pointer back and dropping it.
|
||||||
|
///
|
||||||
|
/// Calling this function is only necessary to achieve composability if you *must*
|
||||||
|
/// map a `&mut SocketRef<'a, XSocket>` to a `&'a mut XSocket` (note the lifetimes);
|
||||||
|
/// be sure to call [wrap] afterwards.
|
||||||
|
///
|
||||||
|
/// [wrap]: #method.wrap
|
||||||
|
pub fn unwrap(mut ref_: Self) -> &'a mut T {
|
||||||
ref_.consumed = true;
|
ref_.consumed = true;
|
||||||
Some(Ref::new(socket))
|
ref_.socket
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
|
||||||
pub fn get<T: AnySocket<'b, 'c>>(&mut self, handle: Handle) -> SocketRef<T> {
|
pub fn get<T: AnySocket<'b, 'c>>(&mut self, handle: Handle) -> SocketRef<T> {
|
||||||
match self.sockets[handle.0].as_mut() {
|
match self.sockets[handle.0].as_mut() {
|
||||||
Some(item) => {
|
Some(item) => {
|
||||||
T::downcast(SocketRef::new(&mut item.socket))
|
T::downcast(SocketRef::wrap(&mut item.socket))
|
||||||
.expect("handle refers to a socket of a wrong type")
|
.expect("handle refers to a socket of a wrong type")
|
||||||
}
|
}
|
||||||
None => panic!("handle does not refer to a valid socket")
|
None => panic!("handle does not refer to a valid socket")
|
||||||
|
@ -209,7 +209,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Iterator for IterMut<'a, 'b, 'c> {
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
while let Some(item_opt) = self.lower.next() {
|
while let Some(item_opt) = self.lower.next() {
|
||||||
if let Some(item) = item_opt.as_mut() {
|
if let Some(item) = item_opt.as_mut() {
|
||||||
return Some(SocketRef::new(&mut item.socket))
|
return Some(SocketRef::wrap(&mut item.socket))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
|
Loading…
Reference in New Issue