1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::{ import::*, RingBuffer };



impl AsyncWrite for RingBuffer<u8>
{
	/// Will return Poll::Pending when the buffer is full. AsyncRead impl will wake up this task
	/// when new place is made.
	/// This method returns a `io::ErrorKind::NotConnected` error if called after `poll_close`.
	//
	fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, src: &[u8] ) -> Poll< Result<usize, io::Error> >
	{
		if self.closed { return Err( io::ErrorKind::NotConnected.into() ).into() }


		match self.producer.push_slice( src )
		{
			Ok(n) =>
			{
				// If a reader is waiting for data, now that we wrote, wake them up.
				//
				if let Some(waker) = self.read_waker.take()
				{
					waker.wake();
				}

				Ok(n).into()
			}


			Err(_) =>
			{
				// If the buffer is full, store our waker so readers can wake us up when they have consumed some data.
				//
				self.write_waker.replace( cx.waker().clone() );

				Poll::Pending
			}
		}
	}


	/// We are always flushed, this is a noop.
	/// This method is infallible.
	//
	fn poll_flush( self: Pin<&mut Self>, _cx: &mut Context<'_> ) -> Poll< Result<(), io::Error> >
	{
		Ok(()).into()
	}


	/// Closes the stream. After this no more data can be send into it.
	/// This method is infallible.
	//
	fn poll_close( mut self: Pin<&mut Self>, _cx: &mut Context<'_> ) -> Poll< Result<(), io::Error> >
	{
		self.closed = true;
		Ok(()).into()
	}
}


#[cfg(test)]
//
mod tests
{
	// What's tested:
	//
	// - ✔ writing to empty buffer
	// - ✔ writing to half full
	// - ✔ writing to full
	// - ✔ setting the waker
	// - ✔ the waker being woken up by a read
	// - ✔ writing again after a read on the full buffer
	// - ✔ writing to a closed buffer
	//
	use crate::{ import::{ *, assert_eq }, RingBuffer };

	#[test]
	//
	fn async_write() { block_on( async
	{
		let mut ring = RingBuffer::<u8>::new(2);

		assert!(  ring.is_empty() );
		assert!( !ring.is_full()  );

		assert_eq!( ring.len()      , 0 );
		assert_eq!( ring.remaining(), 2 );

		assert!( ring.read_waker .is_none() );
		assert!( ring.write_waker.is_none() );


		// write 1
		//
		let arr = [ b'a' ];
		ring.write( &arr ).await.expect( "write" );

		assert!( !ring.is_empty() );
		assert!( !ring.is_full()  );

		assert_eq!( ring.len()      , 1 );
		assert_eq!( ring.remaining(), 1 );

		assert!( ring.read_waker .is_none() );
		assert!( ring.write_waker.is_none() );

		assert_eq!( b'a', ring.consumer.pop().unwrap() );


		// write 2
		//
		let arr = [ b'b' ];
		ring.write( &arr ).await.expect( "write" );

		let arr = [ b'c' ];
		ring.write( &arr ).await.expect( "write" );

		assert!( !ring.is_empty() );
		assert!(  ring.is_full()  );

		assert_eq!( ring.len()      , 2 );
		assert_eq!( ring.remaining(), 0 );

		assert!( ring.read_waker .is_none() );
		assert!( ring.write_waker.is_none() );

		assert_eq!( b'b', ring.consumer.pop().unwrap() );
		assert_eq!( b'c', ring.consumer.pop().unwrap() );


		// write 3
		//
		let arr = [ b'd' ];
		ring.write( &arr ).await.expect( "write" );

		let arr = [ b'e' ];
		ring.write( &arr ).await.expect( "write" );


		let (waker, count) = new_count_waker();
		let mut cx = Context::from_waker( &waker );

		let arr = [ b'f' ];
		assert!( Pin::new( &mut ring ).poll_write( &mut cx, &arr ).is_pending() );

		assert!( !ring.is_empty() );
		assert!(  ring.is_full()  );

		assert_eq!( ring.len()      , 2 );
		assert_eq!( ring.remaining(), 0 );

		assert!( ring.write_waker.is_some() );

		// Pop 1 and try writing again
		//
		let mut read_buf = [0u8;1];
		assert_eq!( 1, ring.read( &mut read_buf ).await.unwrap() );

		assert_eq!( b'd', read_buf[0] );

		assert!( ring.write_waker.is_none() );
		assert_eq!( count, 1 );

		assert!( !ring.is_empty() );
		assert!( !ring.is_full()  );

		assert_eq!( ring.len()      , 1 );
		assert_eq!( ring.remaining(), 1 );


		ring.write( &arr ).await.expect( "write" );

		assert!( !ring.is_empty() );
		assert!(  ring.is_full()  );

		assert_eq!( ring.len()      , 2 );
		assert_eq!( ring.remaining(), 0 );

	})}



	#[test]
	//
	fn closed_write() { block_on( async
	{
		let mut ring = RingBuffer::<u8>::new(2);

		ring.close().await.unwrap();

		let arr = [ b'a' ];
		assert_eq!( ring.write( &arr ).await.unwrap_err().kind(), io::ErrorKind::NotConnected );

		// Should be the same
		//
		assert_eq!( ring.write( &arr ).await.unwrap_err().kind(), io::ErrorKind::NotConnected );
	})}
}