From 2422f2f2245e96fbed61200bb42d04b4e96a32b0 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 11 Aug 2022 12:20:05 +0300 Subject: refactor(tvix/value): hide internal string representation Wraps the string representation in an additional newtype struct with a private field in order to hide the representation from other modules. This is done in order to avoid accidental leakage of the internals outside of value::string. In fact, this caught a mistake in the compiler module which was directly constructing an internal variant. Change-Id: If4b627d3cff7ab9cd50ca1a3ac73245d4dcf7aef Reviewed-on: https://cl.tvl.fyi/c/depot/+/6147 Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/value/string.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'tvix/eval/src/value') diff --git a/tvix/eval/src/value/string.rs b/tvix/eval/src/value/string.rs index 3816e89cd1..3025668218 100644 --- a/tvix/eval/src/value/string.rs +++ b/tvix/eval/src/value/string.rs @@ -5,11 +5,14 @@ use std::{borrow::Cow, fmt::Display}; /// backing implementations. #[derive(Clone, Debug)] -pub enum NixString { +enum StringRepr { Static(&'static str), Heap(String), } +#[derive(Clone, Debug)] +pub struct NixString(StringRepr); + impl PartialEq for NixString { fn eq(&self, other: &Self) -> bool { self.as_str() == other.as_str() @@ -32,13 +35,13 @@ impl Ord for NixString { impl From<&'static str> for NixString { fn from(s: &'static str) -> Self { - NixString::Static(s) + NixString(StringRepr::Static(s)) } } impl From for NixString { fn from(s: String) -> Self { - NixString::Heap(s) + NixString(StringRepr::Heap(s)) } } @@ -49,13 +52,13 @@ impl Hash for NixString { } impl NixString { - pub const NAME: Self = NixString::Static("name"); - pub const VALUE: Self = NixString::Static("value"); + pub const NAME: Self = NixString(StringRepr::Static("name")); + pub const VALUE: Self = NixString(StringRepr::Static("value")); pub fn as_str(&self) -> &str { - match self { - NixString::Static(s) => s, - NixString::Heap(s) => s, + match &self.0 { + StringRepr::Static(s) => s, + StringRepr::Heap(s) => s, } } @@ -82,7 +85,7 @@ impl NixString { pub fn concat(&self, other: &Self) -> Self { let mut s = self.as_str().to_owned(); s.push_str(other.as_str()); - NixString::Heap(s) + NixString(StringRepr::Heap(s)) } } -- cgit 1.4.1