Skip to content

Commit f4e7651

Browse files
authored
Merge pull request #41 from seijikun/master
Update async-std and stop-token dependencies, migrate to stable channels
2 parents 0823e7f + 0e8d3c2 commit f4e7651

File tree

3 files changed

+61
-48
lines changed

3 files changed

+61
-48
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ nom = "5.0"
2727
base64 = "0.13"
2828
chrono = "0.4"
2929
async-native-tls = { version = "0.3.3" }
30-
async-std = { version = "1.6.0", default-features = false, features = ["std"] }
30+
async-std = { version = "1.8.0", default-features = false, features = ["std"] }
3131
pin-utils = "0.1.0-alpha.4"
3232
futures = "0.3.0"
3333
rental = "0.5.5"
34-
stop-token = { version = "0.1.1", features = ["unstable"] }
34+
stop-token = "0.2"
3535
byte-pool = "0.2.2"
3636
lazy_static = "1.4.0"
3737
log = "0.4.8"
@@ -41,7 +41,7 @@ thiserror = "1.0.9"
4141
lettre_email = "0.9"
4242
pretty_assertions = "0.6.1"
4343
async-smtp = { version = "0.3.0" }
44-
async-std = { version = "1.6.0", default-features = false, features = ["std", "attributes"] }
44+
async-std = { version = "1.8.0", default-features = false, features = ["std", "attributes"] }
4545

4646
[[example]]
4747
name = "basic"

src/client.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std::pin::Pin;
55
use std::str;
66

77
use async_native_tls::{TlsConnector, TlsStream};
8+
use async_std::channel;
89
use async_std::io::{self, Read, Write};
910
use async_std::net::{TcpStream, ToSocketAddrs};
1011
use async_std::prelude::*;
11-
use async_std::sync;
1212
use imap_proto::{RequestId, Response};
1313

1414
use super::authenticator::Authenticator;
@@ -36,11 +36,11 @@ macro_rules! quote {
3636
#[derive(Debug)]
3737
pub struct Session<T: Read + Write + Unpin + fmt::Debug> {
3838
pub(crate) conn: Connection<T>,
39-
pub(crate) unsolicited_responses_tx: sync::Sender<UnsolicitedResponse>,
39+
pub(crate) unsolicited_responses_tx: channel::Sender<UnsolicitedResponse>,
4040

4141
/// Server responses that are not related to the current command. See also the note on
4242
/// [unilateral server responses in RFC 3501](https://tools.ietf.org/html/rfc3501#section-7).
43-
pub unsolicited_responses: sync::Receiver<UnsolicitedResponse>,
43+
pub unsolicited_responses: channel::Receiver<UnsolicitedResponse>,
4444
}
4545

4646
impl<T: Read + Write + Unpin + fmt::Debug> Unpin for Session<T> {}
@@ -358,7 +358,7 @@ impl<T: Read + Write + Unpin + fmt::Debug + Send> Session<T> {
358358

359359
// not public, just to avoid duplicating the channel creation code
360360
fn new(conn: Connection<T>) -> Self {
361-
let (tx, rx) = sync::channel(100);
361+
let (tx, rx) = channel::bounded(100);
362362
Session {
363363
conn,
364364
unsolicited_responses: rx,
@@ -1313,7 +1313,7 @@ impl<T: Read + Write + Unpin + fmt::Debug> Connection<T> {
13131313
pub async fn run_command_and_check_ok(
13141314
&mut self,
13151315
command: &str,
1316-
unsolicited: Option<sync::Sender<UnsolicitedResponse>>,
1316+
unsolicited: Option<channel::Sender<UnsolicitedResponse>>,
13171317
) -> Result<()> {
13181318
let id = self.run_command(command).await?;
13191319
self.check_done_ok(&id, unsolicited).await?;
@@ -1324,7 +1324,7 @@ impl<T: Read + Write + Unpin + fmt::Debug> Connection<T> {
13241324
pub(crate) async fn check_done_ok(
13251325
&mut self,
13261326
id: &RequestId,
1327-
unsolicited: Option<sync::Sender<UnsolicitedResponse>>,
1327+
unsolicited: Option<channel::Sender<UnsolicitedResponse>>,
13281328
) -> Result<()> {
13291329
if let Some(first_res) = self.stream.next().await {
13301330
self.check_done_ok_from(id, unsolicited, first_res?).await
@@ -1336,7 +1336,7 @@ impl<T: Read + Write + Unpin + fmt::Debug> Connection<T> {
13361336
pub(crate) async fn check_done_ok_from(
13371337
&mut self,
13381338
id: &RequestId,
1339-
unsolicited: Option<sync::Sender<UnsolicitedResponse>>,
1339+
unsolicited: Option<channel::Sender<UnsolicitedResponse>>,
13401340
mut response: ResponseData,
13411341
) -> Result<()> {
13421342
loop {
@@ -1495,7 +1495,7 @@ mod tests {
14951495
let client = mock_client!(mock_stream);
14961496
enum Authenticate {
14971497
Auth,
1498-
};
1498+
}
14991499
impl Authenticator for &Authenticate {
15001500
type Response = Vec<u8>;
15011501
fn process(&mut self, challenge: &[u8]) -> Self::Response {

src/parse.rs

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use std::collections::HashSet;
22

3+
use async_std::channel;
34
use async_std::io;
45
use async_std::prelude::*;
56
use async_std::stream::Stream;
6-
use async_std::sync;
77
use imap_proto::{self, MailboxDatum, RequestId, Response};
88

99
use crate::error::{Error, Result};
1010
use crate::types::ResponseData;
1111
use crate::types::*;
1212

13-
pub(crate) fn parse_names<'a, T: Stream<Item = io::Result<ResponseData>> + Unpin + Send>(
14-
stream: &'a mut T,
15-
unsolicited: sync::Sender<UnsolicitedResponse>,
13+
pub(crate) fn parse_names<T: Stream<Item = io::Result<ResponseData>> + Unpin + Send>(
14+
stream: &mut T,
15+
unsolicited: channel::Sender<UnsolicitedResponse>,
1616
command_tag: RequestId,
17-
) -> impl Stream<Item = Result<Name>> + 'a + Send + Unpin {
17+
) -> impl Stream<Item = Result<Name>> + '_ + Send + Unpin {
1818
use futures::{FutureExt, StreamExt};
1919

2020
StreamExt::filter_map(
@@ -56,11 +56,11 @@ fn filter_sync(res: &io::Result<ResponseData>, command_tag: &RequestId) -> bool
5656
}
5757
}
5858

59-
pub(crate) fn parse_fetches<'a, T: Stream<Item = io::Result<ResponseData>> + Unpin + Send>(
60-
stream: &'a mut T,
61-
unsolicited: sync::Sender<UnsolicitedResponse>,
59+
pub(crate) fn parse_fetches<T: Stream<Item = io::Result<ResponseData>> + Unpin + Send>(
60+
stream: &mut T,
61+
unsolicited: channel::Sender<UnsolicitedResponse>,
6262
command_tag: RequestId,
63-
) -> impl Stream<Item = Result<Fetch>> + 'a + Send + Unpin {
63+
) -> impl Stream<Item = Result<Fetch>> + '_ + Send + Unpin {
6464
use futures::{FutureExt, StreamExt};
6565

6666
StreamExt::filter_map(
@@ -85,11 +85,11 @@ pub(crate) fn parse_fetches<'a, T: Stream<Item = io::Result<ResponseData>> + Unp
8585
)
8686
}
8787

88-
pub(crate) fn parse_expunge<'a, T: Stream<Item = io::Result<ResponseData>> + Unpin + Send>(
89-
stream: &'a mut T,
90-
unsolicited: sync::Sender<UnsolicitedResponse>,
88+
pub(crate) fn parse_expunge<T: Stream<Item = io::Result<ResponseData>> + Unpin + Send>(
89+
stream: &mut T,
90+
unsolicited: channel::Sender<UnsolicitedResponse>,
9191
command_tag: RequestId,
92-
) -> impl Stream<Item = Result<u32>> + 'a + Send {
92+
) -> impl Stream<Item = Result<u32>> + '_ + Send {
9393
use futures::StreamExt;
9494

9595
StreamExt::filter_map(
@@ -113,9 +113,9 @@ pub(crate) fn parse_expunge<'a, T: Stream<Item = io::Result<ResponseData>> + Unp
113113
)
114114
}
115115

116-
pub(crate) async fn parse_capabilities<'a, T: Stream<Item = io::Result<ResponseData>> + Unpin>(
117-
stream: &'a mut T,
118-
unsolicited: sync::Sender<UnsolicitedResponse>,
116+
pub(crate) async fn parse_capabilities<T: Stream<Item = io::Result<ResponseData>> + Unpin>(
117+
stream: &mut T,
118+
unsolicited: channel::Sender<UnsolicitedResponse>,
119119
command_tag: RequestId,
120120
) -> Result<Capabilities> {
121121
let mut caps: HashSet<Capability> = HashSet::new();
@@ -143,7 +143,7 @@ pub(crate) async fn parse_capabilities<'a, T: Stream<Item = io::Result<ResponseD
143143

144144
pub(crate) async fn parse_noop<T: Stream<Item = io::Result<ResponseData>> + Unpin>(
145145
stream: &mut T,
146-
unsolicited: sync::Sender<UnsolicitedResponse>,
146+
unsolicited: channel::Sender<UnsolicitedResponse>,
147147
command_tag: RequestId,
148148
) -> Result<()> {
149149
while let Some(resp) = stream
@@ -160,7 +160,7 @@ pub(crate) async fn parse_noop<T: Stream<Item = io::Result<ResponseData>> + Unpi
160160

161161
pub(crate) async fn parse_mailbox<T: Stream<Item = io::Result<ResponseData>> + Unpin>(
162162
stream: &mut T,
163-
unsolicited: sync::Sender<UnsolicitedResponse>,
163+
unsolicited: channel::Sender<UnsolicitedResponse>,
164164
command_tag: RequestId,
165165
) -> Result<Mailbox> {
166166
let mut mailbox = Mailbox::default();
@@ -252,7 +252,7 @@ pub(crate) async fn parse_mailbox<T: Stream<Item = io::Result<ResponseData>> + U
252252

253253
pub(crate) async fn parse_ids<T: Stream<Item = io::Result<ResponseData>> + Unpin>(
254254
stream: &mut T,
255-
unsolicited: sync::Sender<UnsolicitedResponse>,
255+
unsolicited: channel::Sender<UnsolicitedResponse>,
256256
command_tag: RequestId,
257257
) -> Result<HashSet<u32>> {
258258
let mut ids: HashSet<u32> = HashSet::new();
@@ -282,7 +282,7 @@ pub(crate) async fn parse_ids<T: Stream<Item = io::Result<ResponseData>> + Unpin
282282
// (see Section 7 of RFC 3501):
283283
pub(crate) async fn handle_unilateral(
284284
res: ResponseData,
285-
unsolicited: sync::Sender<UnsolicitedResponse>,
285+
unsolicited: channel::Sender<UnsolicitedResponse>,
286286
) {
287287
// ignore these if they are not being consumed
288288
if unsolicited.is_full() {
@@ -307,19 +307,32 @@ pub(crate) async fn handle_unilateral(
307307
})
308308
.collect(),
309309
})
310-
.await;
310+
.await
311+
.expect("Channel closed unexpectedly");
311312
}
312313
Response::MailboxData(MailboxDatum::Recent(n)) => {
313-
unsolicited.send(UnsolicitedResponse::Recent(*n)).await;
314+
unsolicited
315+
.send(UnsolicitedResponse::Recent(*n))
316+
.await
317+
.expect("Channel closed unexpectedly");
314318
}
315319
Response::MailboxData(MailboxDatum::Exists(n)) => {
316-
unsolicited.send(UnsolicitedResponse::Exists(*n)).await;
320+
unsolicited
321+
.send(UnsolicitedResponse::Exists(*n))
322+
.await
323+
.expect("Channel closed unexpectedly");
317324
}
318325
Response::Expunge(n) => {
319-
unsolicited.send(UnsolicitedResponse::Expunge(*n)).await;
326+
unsolicited
327+
.send(UnsolicitedResponse::Expunge(*n))
328+
.await
329+
.expect("Channel closed unexpectedly");
320330
}
321331
_ => {
322-
unsolicited.send(UnsolicitedResponse::Other(res)).await;
332+
unsolicited
333+
.send(UnsolicitedResponse::Other(res))
334+
.await
335+
.expect("Channel closed unexpectedly");
323336
}
324337
}
325338
}
@@ -350,7 +363,7 @@ mod tests {
350363
input_stream(&["* CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI LOGINDISABLED\r\n"]);
351364

352365
let mut stream = async_std::stream::from_iter(responses);
353-
let (send, recv) = sync::channel(10);
366+
let (send, recv) = channel::bounded(10);
354367
let id = RequestId("A0001".into());
355368
let capabilities = parse_capabilities(&mut stream, send, id).await.unwrap();
356369
// shouldn't be any unexpected responses parsed
@@ -368,7 +381,7 @@ mod tests {
368381
let responses = input_stream(&["* CAPABILITY IMAP4REV1 STARTTLS\r\n"]);
369382
let mut stream = async_std::stream::from_iter(responses);
370383

371-
let (send, recv) = sync::channel(10);
384+
let (send, recv) = channel::bounded(10);
372385
let id = RequestId("A0001".into());
373386
let capabilities = parse_capabilities(&mut stream, send, id).await.unwrap();
374387

@@ -383,7 +396,7 @@ mod tests {
383396
#[async_std::test]
384397
#[should_panic]
385398
async fn parse_capability_invalid_test() {
386-
let (send, recv) = sync::channel(10);
399+
let (send, recv) = channel::bounded(10);
387400
let responses = input_stream(&["* JUNK IMAP4rev1 STARTTLS AUTH=GSSAPI LOGINDISABLED\r\n"]);
388401
let mut stream = async_std::stream::from_iter(responses);
389402

@@ -396,7 +409,7 @@ mod tests {
396409

397410
#[async_std::test]
398411
async fn parse_names_test() {
399-
let (send, recv) = sync::channel(10);
412+
let (send, recv) = channel::bounded(10);
400413
let responses = input_stream(&["* LIST (\\HasNoChildren) \".\" \"INBOX\"\r\n"]);
401414
let mut stream = async_std::stream::from_iter(responses);
402415

@@ -417,7 +430,7 @@ mod tests {
417430

418431
#[async_std::test]
419432
async fn parse_fetches_empty() {
420-
let (send, recv) = sync::channel(10);
433+
let (send, recv) = channel::bounded(10);
421434
let responses = input_stream(&[]);
422435
let mut stream = async_std::stream::from_iter(responses);
423436
let id = RequestId("a".into());
@@ -432,7 +445,7 @@ mod tests {
432445

433446
#[async_std::test]
434447
async fn parse_fetches_test() {
435-
let (send, recv) = sync::channel(10);
448+
let (send, recv) = channel::bounded(10);
436449
let responses = input_stream(&[
437450
"* 24 FETCH (FLAGS (\\Seen) UID 4827943)\r\n",
438451
"* 25 FETCH (FLAGS (\\Seen))\r\n",
@@ -462,7 +475,7 @@ mod tests {
462475
#[async_std::test]
463476
async fn parse_fetches_w_unilateral() {
464477
// https://github.com/mattnenterprise/rust-imap/issues/81
465-
let (send, recv) = sync::channel(10);
478+
let (send, recv) = channel::bounded(10);
466479
let responses = input_stream(&["* 37 FETCH (UID 74)\r\n", "* 1 RECENT\r\n"]);
467480
let mut stream = async_std::stream::from_iter(responses);
468481
let id = RequestId("a".into());
@@ -480,7 +493,7 @@ mod tests {
480493

481494
#[async_std::test]
482495
async fn parse_names_w_unilateral() {
483-
let (send, recv) = sync::channel(10);
496+
let (send, recv) = channel::bounded(10);
484497
let responses = input_stream(&[
485498
"* LIST (\\HasNoChildren) \".\" \"INBOX\"\r\n",
486499
"* 4 EXPUNGE\r\n",
@@ -506,7 +519,7 @@ mod tests {
506519

507520
#[async_std::test]
508521
async fn parse_capabilities_w_unilateral() {
509-
let (send, recv) = sync::channel(10);
522+
let (send, recv) = channel::bounded(10);
510523
let responses = input_stream(&[
511524
"* CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI LOGINDISABLED\r\n",
512525
"* STATUS dev.github (MESSAGES 10 UIDNEXT 11 UIDVALIDITY 1408806928 UNSEEN 0)\r\n",
@@ -541,7 +554,7 @@ mod tests {
541554

542555
#[async_std::test]
543556
async fn parse_ids_w_unilateral() {
544-
let (send, recv) = sync::channel(10);
557+
let (send, recv) = channel::bounded(10);
545558
let responses = input_stream(&[
546559
"* SEARCH 23 42 4711\r\n",
547560
"* 1 RECENT\r\n",
@@ -571,7 +584,7 @@ mod tests {
571584

572585
#[async_std::test]
573586
async fn parse_ids_test() {
574-
let (send, recv) = sync::channel(10);
587+
let (send, recv) = channel::bounded(10);
575588
let responses = input_stream(&[
576589
"* SEARCH 1600 1698 1739 1781 1795 1885 1891 1892 1893 1898 1899 1901 1911 1926 1932 1933 1993 1994 2007 2032 2033 2041 2053 2062 2063 2065 2066 2072 2078 2079 2082 2084 2095 2100 2101 2102 2103 2104 2107 2116 2120 2135 2138 2154 2163 2168 2172 2189 2193 2198 2199 2205 2212 2213 2221 2227 2267 2275 2276 2295 2300 2328 2330 2332 2333 2334\r\n",
577590
"* SEARCH 2335 2336 2337 2338 2339 2341 2342 2347 2349 2350 2358 2359 2362 2369 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2390 2392 2397 2400 2401 2403 2405 2409 2411 2414 2417 2419 2420 2424 2426 2428 2439 2454 2456 2467 2468 2469 2490 2515 2519 2520 2521\r\n",
@@ -604,7 +617,7 @@ mod tests {
604617

605618
#[async_std::test]
606619
async fn parse_ids_search() {
607-
let (send, recv) = sync::channel(10);
620+
let (send, recv) = channel::bounded(10);
608621
let responses = input_stream(&["* SEARCH\r\n"]);
609622
let mut stream = async_std::stream::from_iter(responses);
610623

0 commit comments

Comments
 (0)