这篇文章上次修改于 908 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

可以用 async 关键字将异步函数进行包裹:

#[test]
    fn async_webm_test() {
        let pieces = vec![
            &ENCODE_WEBM_TEST_FILE[0..20],
            &ENCODE_WEBM_TEST_FILE[20..40],
            &ENCODE_WEBM_TEST_FILE[40..],
        ];

        async {
            let mut parser = futures::stream::iter(pieces.iter())
                .map(|bytes| Ok::<&[u8], WebmetroError>(&bytes[..]))
                .parse_ebml();

            assert_matches!(parser.next().await?, Some(WebmElement::EbmlHead));
            assert_matches!(parser.next().await?, Some(WebmElement::Segment));
            assert_matches!(parser.next().await?, Some(WebmElement::Tracks(_)));
            assert_matches!(parser.next().await?, Some(WebmElement::Cluster));
            assert_matches!(parser.next().await?, Some(WebmElement::Timecode(0)));
            assert_matches!(parser.next().await?, Some(WebmElement::SimpleBlock(_)));
            assert_matches!(parser.next().await?, Some(WebmElement::Cluster));
            assert_matches!(parser.next().await?, Some(WebmElement::Timecode(1000)));

            Result::<(), WebmetroError>::Ok(())
        }
            .now_or_never()
            .expect("Test tried to block on I/O")
            .expect("Parse failed");
    }