Skip to content

Commit 2a3565e

Browse files
ref(chunks): Make ChunkedDifRequest take Cow<'_, str> for name
Also, update the `Assemblable` trait, so the `name` function returns `Cow<'_, str>` This refactor will allow `ChunkedDifRequest` to also store owned types for the `name`, making the type more flexible. We will use this ability when implementing `Assemblable` for `ProguardMapping`
1 parent 543e79e commit 2a3565e

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

src/api/data_types/chunking/dif.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::collections::HashMap;
23

34
use serde::{Deserialize, Serialize};
@@ -10,7 +11,7 @@ use super::ChunkedFileState;
1011

1112
#[derive(Debug, Serialize)]
1213
pub struct ChunkedDifRequest<'a> {
13-
pub name: &'a str,
14+
pub name: Cow<'a, str>,
1415
#[serde(skip_serializing_if = "Option::is_none")]
1516
pub debug_id: Option<DebugId>,
1617
pub chunks: &'a [Digest],
@@ -21,7 +22,7 @@ pub struct ChunkedDifRequest<'a> {
2122
impl<'a> ChunkedDifRequest<'a> {
2223
/// Create a new ChunkedDifRequest with the given name, chunk hashes,
2324
/// and total hash for the entire file.
24-
pub fn new(name: &'a str, chunks: &'a [Digest], hash: Digest) -> Self {
25+
pub fn new(name: Cow<'a, str>, chunks: &'a [Digest], hash: Digest) -> Self {
2526
Self {
2627
name,
2728
chunks,

src/utils/chunks/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Contains data types used in the chunk upload process.
22
3+
use std::borrow::Cow;
34
use std::fmt::{Display, Formatter, Result as FmtResult};
45

56
use anyhow::Result;
@@ -17,7 +18,7 @@ pub type MissingObjectsInfo<'m, T> = (Vec<&'m Chunked<T>>, Vec<Chunk<'m>>);
1718
/// A trait for objects that can be assembled via the `assemble_difs` endpoint.
1819
pub trait Assemblable {
1920
/// Returns the name of the object.
20-
fn name(&self) -> &str;
21+
fn name(&self) -> Cow<'_, str>;
2122

2223
/// Returns the debug ID of the object.
2324
/// Types which do not have a debug ID should return `None`.
@@ -97,7 +98,7 @@ impl<T> Assemblable for Chunked<T>
9798
where
9899
T: Assemblable,
99100
{
100-
fn name(&self) -> &str {
101+
fn name(&self) -> Cow<'_, str> {
101102
self.object().name()
102103
}
103104

src/utils/dif_upload.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Searches, processes and uploads debug information files (DIFs). See
22
//! `DifUpload` for more information.
33
4+
use std::borrow::Cow;
45
use std::collections::{BTreeMap, BTreeSet};
56
use std::convert::TryInto;
67
use std::ffi::{OsStr, OsString};
@@ -306,8 +307,8 @@ impl Display for DifMatch<'_> {
306307

307308
impl Assemblable for DifMatch<'_> {
308309
/// A DIF's name is its file name.
309-
fn name(&self) -> &str {
310-
self.file_name()
310+
fn name(&self) -> Cow<str> {
311+
self.file_name().into()
311312
}
312313

313314
fn debug_id(&self) -> Option<DebugId> {

src/utils/proguard/upload.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! Proguard mappings, while we work on a more permanent solution, which will
55
//! work for all different types of debug files.
66
7+
use std::borrow::Cow;
78
use std::thread;
89
use std::time::{Duration, Instant};
910

@@ -57,7 +58,7 @@ impl ChunkedMapping {
5758

5859
impl<'a> From<&'a ChunkedMapping> for ChunkedDifRequest<'a> {
5960
fn from(value: &'a ChunkedMapping) -> Self {
60-
ChunkedDifRequest::new(&value.file_name, &value.chunk_hashes, value.hash)
61+
ChunkedDifRequest::new(Cow::from(&value.file_name), &value.chunk_hashes, value.hash)
6162
}
6263
}
6364

0 commit comments

Comments
 (0)