fix(tvix): Represent strings as byte arrays

C++ nix uses C-style zero-terminated char pointers to represent strings
internally - however, up to this point, tvix has used Rust `String` and
`str` for string values. Since those are required to be valid utf-8, we
haven't been able to properly represent all the string values that Nix
supports.

To fix that, this change converts the internal representation of the
NixString struct from `Box<str>` to `BString`, from the `bstr` crate -
this is a wrapper around a `Vec<u8>` with extra functions for treating
that byte vector as a "morally string-like" value, which is basically
exactly what we need.

Since this changes a pretty fundamental assumption about a pretty core
type, there are a *lot* of changes in a lot of places to make this work,
but I've tried to keep the general philosophy and intent of most of the
code in most places intact. Most notably, there's nothing that's been
done to make the derivation stuff in //tvix/glue work with non-utf8
strings everywhere, instead opting to just convert to String/str when
passing things into that - there *might* be something to be done there,
but I don't know what the rules should be and I don't want to figure
them out in this change.

To deal with OS-native paths in a way that also works in WASM for
tvixbolt, this also adds a dependency on the "os_str_bytes" crate.

Fixes: b/189
Fixes: b/337
Change-Id: I5e6eb29c62f47dd91af954f5e12bfc3d186f5526
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10200
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: sterni <sternenseemann@systemli.org>
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
This commit is contained in:
Aspen Smith 2023-12-05 17:25:52 -05:00 committed by aspen
parent 6f9e25943f
commit 201173afac
24 changed files with 427 additions and 223 deletions

View file

@ -5,8 +5,10 @@
//!
//! Due to this, construction and management of attribute sets has
//! some peculiarities that are encapsulated within this module.
use std::borrow::Borrow;
use std::iter::FromIterator;
use bstr::BStr;
use imbl::{ordmap, OrdMap};
use lazy_static::lazy_static;
use serde::de::{Deserializer, Error, Visitor};
@ -67,25 +69,25 @@ impl AttrsRep {
}
}
fn select(&self, key: &str) -> Option<&Value> {
fn select(&self, key: &BStr) -> Option<&Value> {
match self {
AttrsRep::Empty => None,
AttrsRep::KV { name, value } => match key {
"name" => Some(name),
"value" => Some(value),
AttrsRep::KV { name, value } => match &**key {
b"name" => Some(name),
b"value" => Some(value),
_ => None,
},
AttrsRep::Im(map) => map.get(&key.into()),
AttrsRep::Im(map) => map.get(key),
}
}
fn contains(&self, key: &str) -> bool {
fn contains(&self, key: &BStr) -> bool {
match self {
AttrsRep::Empty => false,
AttrsRep::KV { .. } => key == "name" || key == "value",
AttrsRep::Im(map) => map.contains_key(&key.into()),
AttrsRep::Im(map) => map.contains_key(key),
}
}
}
@ -264,19 +266,30 @@ impl NixAttrs {
}
/// Select a value from an attribute set by key.
pub fn select(&self, key: &str) -> Option<&Value> {
self.0.select(key)
pub fn select<K>(&self, key: &K) -> Option<&Value>
where
K: Borrow<BStr> + ?Sized,
{
self.0.select(key.borrow())
}
/// Select a required value from an attribute set by key, return
/// an `AttributeNotFound` error if it is missing.
pub fn select_required(&self, key: &str) -> Result<&Value, ErrorKind> {
pub fn select_required<K>(&self, key: &K) -> Result<&Value, ErrorKind>
where
K: Borrow<BStr> + ?Sized,
{
self.select(key)
.ok_or_else(|| ErrorKind::AttributeNotFound { name: key.into() })
.ok_or_else(|| ErrorKind::AttributeNotFound {
name: key.borrow().to_string(),
})
}
pub fn contains(&self, key: &str) -> bool {
self.0.contains(key)
pub fn contains<'a, K: 'a>(&self, key: K) -> bool
where
&'a BStr: From<K>,
{
self.0.contains(key.into())
}
/// Construct an iterator over all the key-value pairs in the attribute set.
@ -423,7 +436,7 @@ fn attempt_optimise_kv(slice: &mut [Value]) -> Option<NixAttrs> {
fn set_attr(attrs: &mut NixAttrs, key: NixString, value: Value) -> Result<(), ErrorKind> {
match attrs.0.map_mut().entry(key) {
imbl::ordmap::Entry::Occupied(entry) => Err(ErrorKind::DuplicateAttrsKey {
key: entry.key().as_str().to_string(),
key: entry.key().to_string(),
}),
imbl::ordmap::Entry::Vacant(entry) => {