From 5663e34331053076a53a63bdab5537c34ba33a89 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Fri, 1 May 2026 11:05:54 +0200 Subject: [PATCH 1/2] feat!: allow storing static data in `CopyDataSource` --- testcontainers/src/core/copy.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/testcontainers/src/core/copy.rs b/testcontainers/src/core/copy.rs index 7c292a2a..3a1cd5e8 100644 --- a/testcontainers/src/core/copy.rs +++ b/testcontainers/src/core/copy.rs @@ -1,4 +1,7 @@ -use std::path::{Path, PathBuf}; +use std::{ + borrow::Cow, + path::{Path, PathBuf}, +}; use async_trait::async_trait; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt}; @@ -22,7 +25,7 @@ pub struct CopyTargetOptions { #[derive(Debug, Clone)] pub enum CopyDataSource { File(PathBuf), - Data(Vec), + Data(Cow<'static, [u8]>), } /// Errors that can occur while materializing data copied from a container. @@ -242,9 +245,10 @@ impl From for CopyDataSource { CopyDataSource::File(value) } } + impl From> for CopyDataSource { fn from(value: Vec) -> Self { - CopyDataSource::Data(value) + CopyDataSource::Data(Cow::Owned(value)) } } @@ -324,7 +328,7 @@ async fn append_tar_file( async fn append_tar_bytes( ar: &mut tokio_tar::Builder>, - data: &Vec, + data: &[u8], target: &CopyTargetOptions, ) -> Result<(), CopyToContainerError> { let relative_target_path = make_path_relative(target.target()); @@ -334,7 +338,7 @@ async fn append_tar_bytes( header.set_mode(target.mode().unwrap_or(0o0644)); header.set_cksum(); - ar.append_data(&mut header, relative_target_path, data.as_slice()) + ar.append_data(&mut header, relative_target_path, data) .await .map_err(CopyToContainerError::IoError)?; From d7ae6afdb29bd2b2dd7a8c699c7c302e04bd7038 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Fri, 1 May 2026 11:06:56 +0200 Subject: [PATCH 2/2] feat: add `From<{&'static u8,&'static str,String}>` implementations for `CopyDataSource` --- testcontainers/src/core/copy.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/testcontainers/src/core/copy.rs b/testcontainers/src/core/copy.rs index 3a1cd5e8..77c7b437 100644 --- a/testcontainers/src/core/copy.rs +++ b/testcontainers/src/core/copy.rs @@ -252,6 +252,24 @@ impl From> for CopyDataSource { } } +impl From for CopyDataSource { + fn from(value: String) -> Self { + CopyDataSource::Data(Cow::Owned(value.into_bytes())) + } +} + +impl From<&'static [u8]> for CopyDataSource { + fn from(value: &'static [u8]) -> Self { + CopyDataSource::Data(Cow::Borrowed(value)) + } +} + +impl From<&'static str> for CopyDataSource { + fn from(value: &'static str) -> Self { + CopyDataSource::Data(Cow::Borrowed(value.as_bytes())) + } +} + impl CopyDataSource { pub(crate) async fn append_tar( &self,