Skip to content

Commit d13f77e

Browse files
taiki-ecramertj
authored andcommitted
Add get_ref/get_mut/get_pin_mut/into_inner methods
1 parent 87d08f0 commit d13f77e

34 files changed

+481
-8
lines changed

futures-util/src/sink/buffer.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ impl<Si: Sink<Item>, Item> Buffer<Si, Item> {
3636
&self.sink
3737
}
3838

39+
/// Get a mutable reference to the inner sink.
40+
pub fn get_mut(&mut self) -> &mut Si {
41+
&mut self.sink
42+
}
43+
44+
/// Get a pinned mutable reference to the inner sink.
45+
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut Si> {
46+
unsafe { Pin::map_unchecked_mut(self, Self::get_mut) }
47+
}
48+
49+
/// Consumes this combinator, returning the underlying sink.
50+
///
51+
/// Note that this may discard intermediate state of this combinator, so
52+
/// care should be taken to avoid losing resources when this is called.
53+
pub fn into_inner(self) -> Si {
54+
self.sink
55+
}
56+
3957
fn try_empty_buffer(
4058
mut self: Pin<&mut Self>,
4159
cx: &mut Context<'_>,

futures-util/src/sink/err_into.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ impl<Si, E, Item> SinkErrInto<Si, Item, E>
3434
self.sink.get_mut()
3535
}
3636

37+
/// Get a pinned mutable reference to the inner sink.
38+
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut Si> {
39+
unsafe { Pin::map_unchecked_mut(self, Self::get_mut) }
40+
}
41+
3742
/// Consumes this combinator, returning the underlying sink.
3843
///
3944
/// Note that this may discard intermediate state of this combinator, so

futures-util/src/sink/fanout.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ impl<Si1, Si2> Fanout<Si1, Si2> {
2121
Fanout { sink1, sink2 }
2222
}
2323

24+
/// Get a shared reference to the inner sinks.
25+
pub fn get_ref(&self) -> (&Si1, &Si2) {
26+
(&self.sink1, &self.sink2)
27+
}
28+
29+
/// Get a mutable reference to the inner sinks.
30+
pub fn get_mut(&mut self) -> (&mut Si1, &mut Si2) {
31+
(&mut self.sink1, &mut self.sink2)
32+
}
33+
34+
/// Get a pinned mutable reference to the inner sinks.
35+
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> (Pin<&'a mut Si1>, Pin<&'a mut Si2>)
36+
where Si1: Unpin, Si2: Unpin,
37+
{
38+
let Self { sink1, sink2 } = Pin::get_mut(self);
39+
(Pin::new(sink1), Pin::new(sink2))
40+
}
41+
2442
/// Consumes this combinator, returning the underlying sinks.
2543
///
2644
/// Note that this may discard intermediate state of this combinator,

futures-util/src/sink/map_err.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ impl<Si, F> SinkMapErr<Si, F> {
3232
&mut self.sink
3333
}
3434

35-
/// Get a pinned reference to the inner sink.
35+
/// Get a pinned mutable reference to the inner sink.
3636
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut Si> {
37-
unsafe { Pin::map_unchecked_mut(self, |x| &mut x.sink) }
37+
unsafe { Pin::map_unchecked_mut(self, Self::get_mut) }
3838
}
3939

4040
/// Consumes this combinator, returning the underlying sink.

futures-util/src/sink/with.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ impl<Si, Item, U, Fut, F, E> With<Si, Item, U, Fut, F>
107107
&mut self.sink
108108
}
109109

110+
/// Get a pinned mutable reference to the inner sink.
111+
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut Si> {
112+
unsafe { Pin::map_unchecked_mut(self, Self::get_mut) }
113+
}
114+
110115
/// Consumes this combinator, returning the underlying sink.
111116
///
112117
/// Note that this may discard intermediate state of this combinator, so

futures-util/src/sink/with_flat_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ where
6060

6161
/// Get a pinned mutable reference to the inner sink.
6262
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut Si> {
63-
unsafe { Pin::map_unchecked_mut(self, |x| &mut x.sink) }
63+
unsafe { Pin::map_unchecked_mut(self, Self::get_mut) }
6464
}
6565

6666
/// Consumes this combinator, returning the underlying sink.

futures-util/src/stream/buffer_unordered.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ where
8181
/// Note that care must be taken to avoid tampering with the state of the
8282
/// stream which may otherwise confuse this combinator.
8383
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut St> {
84-
unsafe { Pin::map_unchecked_mut(self, |x| x.get_mut()) }
84+
unsafe { Pin::map_unchecked_mut(self, Self::get_mut) }
8585
}
8686

8787
/// Consumes this combinator, returning the underlying stream.

futures-util/src/stream/buffered.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ where
7070
self.stream.get_mut()
7171
}
7272

73-
/// Acquires a mutable pinned reference to the underlying stream that this
73+
/// Acquires a pinned mutable reference to the underlying stream that this
7474
/// combinator is pulling from.
7575
///
7676
/// Note that care must be taken to avoid tampering with the state of the
7777
/// stream which may otherwise confuse this combinator.
7878
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut St> {
79-
unsafe { Pin::map_unchecked_mut(self, |x| x.get_mut()) }
79+
unsafe { Pin::map_unchecked_mut(self, Self::get_mut) }
8080
}
8181

8282
/// Consumes this combinator, returning the underlying stream.

futures-util/src/stream/chunks.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ impl<St: Stream> Chunks<St> where St: Stream {
5252
self.stream.get_mut()
5353
}
5454

55+
/// Acquires a pinned mutable reference to the underlying stream that this
56+
/// combinator is pulling from.
57+
///
58+
/// Note that care must be taken to avoid tampering with the state of the
59+
/// stream which may otherwise confuse this combinator.
60+
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut St> {
61+
unsafe { Pin::map_unchecked_mut(self, Self::get_mut) }
62+
}
63+
5564
/// Consumes this combinator, returning the underlying stream.
5665
///
5766
/// Note that this may discard intermediate state of this combinator, so

futures-util/src/stream/enumerate.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,38 @@ impl<St: Stream> Enumerate<St> {
2424
count: 0,
2525
}
2626
}
27+
28+
/// Acquires a reference to the underlying stream that this combinator is
29+
/// pulling from.
30+
pub fn get_ref(&self) -> &St {
31+
&self.stream
32+
}
33+
34+
/// Acquires a mutable reference to the underlying stream that this
35+
/// combinator is pulling from.
36+
///
37+
/// Note that care must be taken to avoid tampering with the state of the
38+
/// stream which may otherwise confuse this combinator.
39+
pub fn get_mut(&mut self) -> &mut St {
40+
&mut self.stream
41+
}
42+
43+
/// Acquires a pinned mutable reference to the underlying stream that this
44+
/// combinator is pulling from.
45+
///
46+
/// Note that care must be taken to avoid tampering with the state of the
47+
/// stream which may otherwise confuse this combinator.
48+
pub fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> Pin<&'a mut St> {
49+
unsafe { Pin::map_unchecked_mut(self, Self::get_mut) }
50+
}
51+
52+
/// Consumes this combinator, returning the underlying stream.
53+
///
54+
/// Note that this may discard intermediate state of this combinator, so
55+
/// care should be taken to avoid losing resources when this is called.
56+
pub fn into_inner(self) -> St {
57+
self.stream
58+
}
2759
}
2860

2961
impl<St: Stream + FusedStream> FusedStream for Enumerate<St> {

0 commit comments

Comments
 (0)