feat(tvix/value): introduce string representation with &'static str
For cases where the strings are statically known (such as the oft-occuring name/value), this can be a useful optimisation. It's also much more convenient in tests. Change-Id: Ie462b684805bd4986ea5e85ca4bff663bc2d3c3c Reviewed-on: https://cl.tvl.fyi/c/depot/+/6111 Tested-by: BuildkiteCI Reviewed-by: eta <tvl@eta.st>
This commit is contained in:
		
							parent
							
								
									c7ba2dec04
								
							
						
					
					
						commit
						6dc9ca5723
					
				
					 4 changed files with 44 additions and 26 deletions
				
			
		|  | @ -4,16 +4,40 @@ use std::fmt::Display; | |||
| /// backing implementations.
 | ||||
| 
 | ||||
| #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] | ||||
| pub struct NixString(pub String); | ||||
| pub enum NixString { | ||||
|     Static(&'static str), | ||||
|     Heap(String), | ||||
| } | ||||
| 
 | ||||
| impl Display for NixString { | ||||
|     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||||
|         f.write_str(self.0.as_str()) | ||||
|         match self { | ||||
|             NixString::Static(s) => f.write_str(s), | ||||
|             NixString::Heap(s) => f.write_str(s), | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl From<&str> for NixString { | ||||
|     fn from(s: &str) -> Self { | ||||
|         NixString(s.to_string()) | ||||
| impl From<&'static str> for NixString { | ||||
|     fn from(s: &'static str) -> Self { | ||||
|         NixString::Static(s) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl From<String> for NixString { | ||||
|     fn from(s: String) -> Self { | ||||
|         NixString::Heap(s) | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl NixString { | ||||
|     pub const NAME: Self = NixString::Static("name"); | ||||
|     pub const VALUE: Self = NixString::Static("value"); | ||||
| 
 | ||||
|     pub fn as_str(&self) -> &str { | ||||
|         match self { | ||||
|             NixString::Static(s) => s, | ||||
|             NixString::Heap(s) => s, | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue