From cb3d07ec26425599d6e5790bd656cdd2b36aaab3 Mon Sep 17 00:00:00 2001 From: davidemazzocchi Date: Mon, 16 Mar 2026 11:02:42 +0100 Subject: [PATCH] chore: avoid cloning entity id --- 01.workspace/heave/examples/simple_product.rs | 2 +- 01.workspace/heave/examples/using_filters.rs | 2 +- 01.workspace/heave/examples/working_with_many_types.rs | 6 +++--- 01.workspace/heave/src/lib.rs | 2 +- 01.workspace/heave/src/str/entity.rs | 4 ++-- 01.workspace/heave/src/str/item.rs | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/01.workspace/heave/examples/simple_product.rs b/01.workspace/heave/examples/simple_product.rs index 0a08169..993da42 100644 --- a/01.workspace/heave/examples/simple_product.rs +++ b/01.workspace/heave/examples/simple_product.rs @@ -46,7 +46,7 @@ impl From for Product { // Create a new `Product` from the entity's attributes. Self { // Set the product's ID from the entity's ID. - id: value.id(), + id: value.id().to_string(), // Unwrap the "name" attribute to get the product's name. name: value.unwrap("name").expect("name is always present"), // Unwrap the optional "model" attribute to get the product's model. diff --git a/01.workspace/heave/examples/using_filters.rs b/01.workspace/heave/examples/using_filters.rs index 28c0f93..bdc0146 100644 --- a/01.workspace/heave/examples/using_filters.rs +++ b/01.workspace/heave/examples/using_filters.rs @@ -37,7 +37,7 @@ impl From for Component { // `from` is a function that converts an `Entity` into a `Component`. fn from(value: Entity) -> Self { Self { - id: value.id(), + id: value.id().to_string(), part_number: value .unwrap("part_number") .expect("part_number is always present"), diff --git a/01.workspace/heave/examples/working_with_many_types.rs b/01.workspace/heave/examples/working_with_many_types.rs index bad3015..ae59513 100644 --- a/01.workspace/heave/examples/working_with_many_types.rs +++ b/01.workspace/heave/examples/working_with_many_types.rs @@ -41,18 +41,18 @@ impl From for Product { if let Some(ref subclass) = value.subclass() { match subclass.as_ref() { "laptop" => Product::Laptop(Laptop { - id: value.id(), + id: value.id().to_string(), model: value.unwrap("model").expect("model is mandatory"), price: value.unwrap("price").expect("price is mandatory"), }), "display" => Product::Display(Display { - id: value.id(), + id: value.id().to_string(), model: value.unwrap("model").expect("model is mandatory"), price: value.unwrap("price").expect("price is mandatory"), resolution: value.unwrap("resolution").expect("resolution is mandatory"), }), "mouse" => Product::Mouse(Mouse { - id: value.id(), + id: value.id().to_string(), model: value.unwrap("model").expect("model is mandatory"), price: value.unwrap("price").expect("price is mandatory"), wireless: value.unwrap("wireless").expect("wireless is mandatory"), diff --git a/01.workspace/heave/src/lib.rs b/01.workspace/heave/src/lib.rs index 6f66775..1d7e4d6 100644 --- a/01.workspace/heave/src/lib.rs +++ b/01.workspace/heave/src/lib.rs @@ -73,7 +73,7 @@ //! //! fn try_from(entity: Entity) -> Result { //! Ok(Self { -//! id: entity.id(), +//! id: entity.id().to_string(), //! name: entity.unwrap("name").map_err(|_| FailedTo::ConvertEntity)?, //! price: entity.unwrap("price").map_err(|_| FailedTo::ConvertEntity)?, //! in_stock: entity.unwrap("in_stock").map_err(|_| FailedTo::ConvertEntity)?, diff --git a/01.workspace/heave/src/str/entity.rs b/01.workspace/heave/src/str/entity.rs index b4114f1..7a127de 100644 --- a/01.workspace/heave/src/str/entity.rs +++ b/01.workspace/heave/src/str/entity.rs @@ -195,8 +195,8 @@ impl Entity { .map(|value| value.unwrap_or(default)) } /// Returns the ID of the entity. - pub fn id(&self) -> String { - self.id.clone() + pub fn id(&self) -> &str { + &self.id } /// Returns the subclass of the entity, if it has one. pub fn subclass(&self) -> Option { diff --git a/01.workspace/heave/src/str/item.rs b/01.workspace/heave/src/str/item.rs index 08b532b..1a946cf 100644 --- a/01.workspace/heave/src/str/item.rs +++ b/01.workspace/heave/src/str/item.rs @@ -48,7 +48,7 @@ impl From for Entity { impl From for Item { fn from(entity: Entity) -> Self { Self { - id: entity.id(), + id: entity.id().to_string(), first_seen: entity.ref_date.expect("ref date is always present"), name: entity.unwrap("name").expect("name is always present"), price: entity.unwrap("price").expect("price is always present"),