diff options
author | Vova Kryachko <v.kryachko@gmail.com> | 2024-11-10T16·16-0500 |
---|---|---|
committer | Vladimir Kryachko <v.kryachko@gmail.com> | 2024-11-10T20·54+0000 |
commit | 8df919dcf04b5c2502f3a63b4d013669da5e70c1 (patch) | |
tree | 3a527553fc48f62f331ef2b6a5667b6fe591290d /tvix/nix-compat/src/nix_daemon/ser/bytes.rs | |
parent | 11ee751aff42804319788a9033685a455bdf8f8e (diff) |
refactor(nix-compat): Move serialization machinery into wire. r/8898
This groups most `wire` feature gated logic into a single module. The nix_daemon module will be gated by a feature that adds nix-compat-derive as a dependency. All of this is a way to break the crate2nix dependency cycle between nix-compat and nix-compat-derive(which depends on nix-compat for its doctests). Change-Id: I95938a6f280c11967371ff21f8b5a19e6d3d3805 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12761 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nix-compat/src/nix_daemon/ser/bytes.rs')
-rw-r--r-- | tvix/nix-compat/src/nix_daemon/ser/bytes.rs | 89 |
1 files changed, 0 insertions, 89 deletions
diff --git a/tvix/nix-compat/src/nix_daemon/ser/bytes.rs b/tvix/nix-compat/src/nix_daemon/ser/bytes.rs deleted file mode 100644 index 19494934ff32..000000000000 --- a/tvix/nix-compat/src/nix_daemon/ser/bytes.rs +++ /dev/null @@ -1,89 +0,0 @@ -use bytes::Bytes; - -use super::{NixSerialize, NixWrite}; - -impl NixSerialize for Bytes { - async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error> - where - W: NixWrite, - { - writer.write_slice(self).await - } -} - -impl<'a> NixSerialize for &'a [u8] { - async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error> - where - W: NixWrite, - { - writer.write_slice(self).await - } -} - -impl NixSerialize for String { - async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error> - where - W: NixWrite, - { - writer.write_slice(self.as_bytes()).await - } -} - -impl NixSerialize for str { - async fn serialize<W>(&self, writer: &mut W) -> Result<(), W::Error> - where - W: NixWrite, - { - writer.write_slice(self.as_bytes()).await - } -} - -#[cfg(test)] -mod test { - use hex_literal::hex; - use rstest::rstest; - use tokio::io::AsyncWriteExt as _; - use tokio_test::io::Builder; - - use crate::nix_daemon::ser::{NixWrite, NixWriter}; - - #[rstest] - #[case::empty("", &hex!("0000 0000 0000 0000"))] - #[case::one(")", &hex!("0100 0000 0000 0000 2900 0000 0000 0000"))] - #[case::two("it", &hex!("0200 0000 0000 0000 6974 0000 0000 0000"))] - #[case::three("tea", &hex!("0300 0000 0000 0000 7465 6100 0000 0000"))] - #[case::four("were", &hex!("0400 0000 0000 0000 7765 7265 0000 0000"))] - #[case::five("where", &hex!("0500 0000 0000 0000 7768 6572 6500 0000"))] - #[case::six("unwrap", &hex!("0600 0000 0000 0000 756E 7772 6170 0000"))] - #[case::seven("where's", &hex!("0700 0000 0000 0000 7768 6572 6527 7300"))] - #[case::aligned("read_tea", &hex!("0800 0000 0000 0000 7265 6164 5F74 6561"))] - #[case::more_bytes("read_tess", &hex!("0900 0000 0000 0000 7265 6164 5F74 6573 7300 0000 0000 0000"))] - #[case::utf8("The quick brown 🦊 jumps over 13 lazy 🐶.", &hex!("2D00 0000 0000 0000 5468 6520 7175 6963 6b20 6272 6f77 6e20 f09f a68a 206a 756d 7073 206f 7665 7220 3133 206c 617a 7920 f09f 90b6 2e00 0000"))] - #[tokio::test] - async fn test_write_str(#[case] value: &str, #[case] data: &[u8]) { - let mock = Builder::new().write(data).build(); - let mut writer = NixWriter::new(mock); - writer.write_value(value).await.unwrap(); - writer.flush().await.unwrap(); - } - - #[rstest] - #[case::empty("", &hex!("0000 0000 0000 0000"))] - #[case::one(")", &hex!("0100 0000 0000 0000 2900 0000 0000 0000"))] - #[case::two("it", &hex!("0200 0000 0000 0000 6974 0000 0000 0000"))] - #[case::three("tea", &hex!("0300 0000 0000 0000 7465 6100 0000 0000"))] - #[case::four("were", &hex!("0400 0000 0000 0000 7765 7265 0000 0000"))] - #[case::five("where", &hex!("0500 0000 0000 0000 7768 6572 6500 0000"))] - #[case::six("unwrap", &hex!("0600 0000 0000 0000 756E 7772 6170 0000"))] - #[case::seven("where's", &hex!("0700 0000 0000 0000 7768 6572 6527 7300"))] - #[case::aligned("read_tea", &hex!("0800 0000 0000 0000 7265 6164 5F74 6561"))] - #[case::more_bytes("read_tess", &hex!("0900 0000 0000 0000 7265 6164 5F74 6573 7300 0000 0000 0000"))] - #[case::utf8("The quick brown 🦊 jumps over 13 lazy 🐶.", &hex!("2D00 0000 0000 0000 5468 6520 7175 6963 6b20 6272 6f77 6e20 f09f a68a 206a 756d 7073 206f 7665 7220 3133 206c 617a 7920 f09f 90b6 2e00 0000"))] - #[tokio::test] - async fn test_write_string(#[case] value: &str, #[case] data: &[u8]) { - let mock = Builder::new().write(data).build(); - let mut writer = NixWriter::new(mock); - writer.write_value(&value.to_string()).await.unwrap(); - writer.flush().await.unwrap(); - } -} |