From 45cf7ae657086993cedaa7c72b813e319e805484 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Wed, 10 Apr 2024 16:33:02 +0300 Subject: refactor(tvix/nix-compat): move nar writer to tokio There's little reason to keep the nar writer using Async{Read,Write} traits from futures, while everything else async in tvix (and nix-compat) uses tokio. Change-Id: I8cd1efcd0dd5bb76471de997603c7b701a5095de Reviewed-on: https://cl.tvl.fyi/c/depot/+/11391 Tested-by: BuildkiteCI Reviewed-by: raitobezarius Reviewed-by: Brian Olsen --- tvix/nix-compat/src/nar/writer/async.rs | 4 +- tvix/nix-compat/src/nar/writer/test.rs | 103 +++++++++++++++----------------- 2 files changed, 51 insertions(+), 56 deletions(-) (limited to 'tvix/nix-compat/src') diff --git a/tvix/nix-compat/src/nar/writer/async.rs b/tvix/nix-compat/src/nar/writer/async.rs index 11aefab9cba7..a2ce68fc3c9e 100644 --- a/tvix/nix-compat/src/nar/writer/async.rs +++ b/tvix/nix-compat/src/nar/writer/async.rs @@ -10,7 +10,7 @@ //! //! ```rust //! # futures::executor::block_on(async { -//! # use futures::io::BufReader; +//! # use tokio::io::BufReader; //! # let some_file: Vec = vec![0, 1, 2, 3, 4]; //! //! // Output location to write the NAR to. @@ -31,7 +31,6 @@ //! ``` use crate::nar::wire; -use futures_util::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt}; use std::{ io::{ self, @@ -39,6 +38,7 @@ use std::{ }, pin::Pin, }; +use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt}; /// Convenience type alias for types implementing [`AsyncWrite`]. pub type Writer<'a> = dyn AsyncWrite + Unpin + Send + 'a; diff --git a/tvix/nix-compat/src/nar/writer/test.rs b/tvix/nix-compat/src/nar/writer/test.rs index 7b1dd1a2a96e..d7f18a49af34 100644 --- a/tvix/nix-compat/src/nar/writer/test.rs +++ b/tvix/nix-compat/src/nar/writer/test.rs @@ -11,16 +11,14 @@ fn symlink() { } #[cfg(feature = "async")] -#[test] -fn symlink_async() { +#[tokio::test] +async fn symlink_async() { let mut buf = vec![]; - futures::executor::block_on(async { - let node = nar::writer::r#async::open(&mut buf).await.unwrap(); - node.symlink("/nix/store/somewhereelse".as_bytes()) - .await - .unwrap(); - }); + let node = nar::writer::r#async::open(&mut buf).await.unwrap(); + node.symlink("/nix/store/somewhereelse".as_bytes()) + .await + .unwrap(); assert_eq!(include_bytes!("../tests/symlink.nar"), buf.as_slice()); } @@ -42,22 +40,22 @@ fn file() { } #[cfg(feature = "async")] -#[test] -fn file_async() { +#[tokio::test] +async fn file_async() { + use std::io::Cursor; + let mut buf = vec![]; - futures::executor::block_on(async { - let node = nar::writer::r#async::open(&mut buf).await.unwrap(); + let node = nar::writer::r#async::open(&mut buf).await.unwrap(); - let file_contents = "Hello World!".to_string(); - node.file( - false, - file_contents.len() as u64, - &mut futures::io::Cursor::new(file_contents), - ) - .await - .unwrap(); - }); + let file_contents = "Hello World!".to_string(); + node.file( + false, + file_contents.len() as u64, + &mut Cursor::new(file_contents), + ) + .await + .unwrap(); assert_eq!(include_bytes!("../tests/helloworld.nar"), buf.as_slice()); } @@ -93,41 +91,38 @@ fn complicated() { } #[cfg(feature = "async")] -#[test] -fn complicated_async() { +#[tokio::test] +async fn complicated_async() { + use std::io::Cursor; + let mut buf = vec![]; - futures::executor::block_on(async { - let node = nar::writer::r#async::open(&mut buf).await.unwrap(); - - let mut dir_node = node.directory().await.unwrap(); - - let e = dir_node.entry(".keep".as_bytes()).await.unwrap(); - e.file(false, 0, &mut futures::io::Cursor::new([])) - .await - .expect("read .keep must succeed"); - - let e = dir_node.entry("aa".as_bytes()).await.unwrap(); - e.symlink("/nix/store/somewhereelse".as_bytes()) - .await - .expect("symlink must succeed"); - - let e = dir_node.entry("keep".as_bytes()).await.unwrap(); - let mut subdir_node = e.directory().await.expect("directory must succeed"); - - let e_sub = subdir_node - .entry(".keep".as_bytes()) - .await - .expect("subdir entry must succeed"); - e_sub - .file(false, 0, &mut futures::io::Cursor::new([])) - .await - .unwrap(); - - // close the subdir, and then the dir, which is required. - subdir_node.close().await.unwrap(); - dir_node.close().await.unwrap(); - }); + let node = nar::writer::r#async::open(&mut buf).await.unwrap(); + + let mut dir_node = node.directory().await.unwrap(); + + let e = dir_node.entry(".keep".as_bytes()).await.unwrap(); + e.file(false, 0, &mut Cursor::new([])) + .await + .expect("read .keep must succeed"); + + let e = dir_node.entry("aa".as_bytes()).await.unwrap(); + e.symlink("/nix/store/somewhereelse".as_bytes()) + .await + .expect("symlink must succeed"); + + let e = dir_node.entry("keep".as_bytes()).await.unwrap(); + let mut subdir_node = e.directory().await.expect("directory must succeed"); + + let e_sub = subdir_node + .entry(".keep".as_bytes()) + .await + .expect("subdir entry must succeed"); + e_sub.file(false, 0, &mut Cursor::new([])).await.unwrap(); + + // close the subdir, and then the dir, which is required. + subdir_node.close().await.unwrap(); + dir_node.close().await.unwrap(); assert_eq!(include_bytes!("../tests/complicated.nar"), buf.as_slice()); } -- cgit 1.4.1