Skip to content

Commit 037549c

Browse files
authored
Make UploadableFile::get_data async for AsyncRead (#82)
2 parents 1bddd81 + e0173d2 commit 037549c

File tree

6 files changed

+15
-12
lines changed

6 files changed

+15
-12
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gitlab-runner/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bytes = "1.9.0"
2424
zip = "2.2.2"
2525
pin-project = "1.0.7"
2626
futures = "0.3.15"
27-
async-trait = "0.1.50"
27+
async-trait = "0.1.86"
2828
tempfile = "3.15.0"
2929
parking_lot = "0.12.0"
3030
tracing-subscriber = "0.3.10"

gitlab-runner/examples/demo-runner.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ enum DemoFile {
141141
Fact { index: usize, value: String },
142142
}
143143

144+
#[async_trait::async_trait]
144145
impl UploadableFile for DemoFile {
145146
type Data<'a> = Box<dyn AsyncRead + Send + Unpin>;
146147

@@ -151,11 +152,11 @@ impl UploadableFile for DemoFile {
151152
}
152153
}
153154

154-
fn get_data(&self) -> Box<dyn AsyncRead + Send + Unpin> {
155-
match self {
155+
async fn get_data(&self) -> Result<Box<dyn AsyncRead + Send + Unpin>, ()> {
156+
Ok(match self {
156157
DemoFile::ReadMe => Box::new(b"Demo runner artifact\n".as_slice()),
157158
DemoFile::Fact { value, .. } => Box::new(Cursor::new(value.clone().into_bytes())),
158-
}
159+
})
159160
}
160161
}
161162

gitlab-runner/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub use client::Phase;
5959
/// generated or downloaded from some other source on demand. The
6060
/// `get_path` method is required so that the globbing Gitlab expects
6161
/// can be performed without the handler needing to be involved.
62+
#[async_trait::async_trait]
6263
pub trait UploadableFile: Eq {
6364
/// The type of the data stream returned by
6465
/// [`get_data`](Self::get_data)
@@ -76,7 +77,7 @@ pub trait UploadableFile: Eq {
7677
/// Get something that can provide the data for this file.
7778
///
7879
/// This can be any implementor of [`AsyncRead`].
79-
fn get_data(&self) -> Self::Data<'_>;
80+
async fn get_data(&self) -> Result<Self::Data<'_>, ()>;
8081
}
8182

8283
/// An [`UploadableFile`] type for JobHandlers that expose no files.
@@ -87,13 +88,14 @@ pub trait UploadableFile: Eq {
8788
#[derive(Clone, Debug, Eq, PartialEq)]
8889
pub struct NoFiles {}
8990

91+
#[async_trait::async_trait]
9092
impl UploadableFile for NoFiles {
9193
type Data<'a> = &'a [u8];
9294

9395
fn get_path(&self) -> Cow<'_, str> {
9496
unreachable!("tried to get path of NoFiles")
9597
}
96-
fn get_data(&self) -> Self::Data<'_> {
98+
async fn get_data(&self) -> Result<Self::Data<'_>, ()> {
9799
unreachable!("tried to read data from NoFiles");
98100
}
99101
}

gitlab-runner/src/run.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ where
135135
let path = file.get_path();
136136
match uploader.file(path.to_string()).await {
137137
Ok(mut upload) => {
138-
let mut data = file.get_data();
138+
let mut data = file.get_data().await?;
139139
match futures::io::copy(&mut data, &mut upload).await {
140140
Ok(_) => {
141141
uploaded += 1;

gitlab-runner/tests/artifacts.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ impl UploadableFile for TestFile {
2929
TestFile::Test2 => "test2".into(),
3030
}
3131
}
32-
fn get_data(&self) -> &'_ [u8] {
33-
match self {
32+
async fn get_data(&self) -> Result<&'_ [u8], ()> {
33+
Ok(match self {
3434
TestFile::Test => b"testdata".as_slice(),
3535
TestFile::Test2 => b"testdata2".as_slice(),
36-
}
36+
})
3737
}
3838
}
3939

0 commit comments

Comments
 (0)