From fd18830005027dfd87f4d661411bff740ce88a4a Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Wed, 18 Aug 2021 02:29:40 -0500 Subject: [PATCH] Rewrite resource_path_to_string for a small code size improvement --- Cargo.lock | 1 + provider/blob/Cargo.toml | 1 + provider/blob/src/path_util.rs | 13 +++++++------ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0f384f12d8e..a2cbfb5c055 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1162,6 +1162,7 @@ dependencies = [ "log", "postcard", "serde", + "writeable", ] [[package]] diff --git a/provider/blob/Cargo.toml b/provider/blob/Cargo.toml index 66798438c1c..a21741dd821 100644 --- a/provider/blob/Cargo.toml +++ b/provider/blob/Cargo.toml @@ -32,6 +32,7 @@ serde = { version = "1.0", default-features = false, features = ["alloc"] } postcard = { version = "0.7.0" } erased-serde = { version = "0.3", default-features = false, features = ["alloc"] } litemap = { version = "0.2.0", path = "../../utils/litemap/", features = ["serde"] } +writeable = { path = "../../utils/writeable" } # For the export feature log = { version = "0.4", optional = true } diff --git a/provider/blob/src/path_util.rs b/provider/blob/src/path_util.rs index 8ebf8084699..b0feeae4f0b 100644 --- a/provider/blob/src/path_util.rs +++ b/provider/blob/src/path_util.rs @@ -2,13 +2,14 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -use alloc::string::{String, ToString}; -use alloc::vec::Vec; +use alloc::string::String; use icu_provider::prelude::*; +use writeable::Writeable; pub fn resource_path_to_string(resource_path: &ResourcePath) -> String { - let key_components = resource_path.key.get_components(); - let opt_components = resource_path.options.get_components(); - let all_components: Vec<&str> = key_components.iter().chain(opt_components.iter()).collect(); - "/".to_string() + &all_components.join("/") + let mut output = String::with_capacity(resource_path.write_len().capacity() + 1); + output.push('/'); + resource_path.write_to(&mut output) + .expect("impl Write for String is infallible"); + output }