Skip to content

remove byteyarn #1056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 3 additions & 16 deletions gitoxide-core/src/repository/index/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,7 @@ pub(crate) mod function {
Ok((pathspec.into_parts().0, index, cache))
}

#[cfg(feature = "serde")]
#[derive(serde::Serialize)]
struct AttrsForSerde {
is_excluded: bool,
attributes: Vec<String>,
}

#[cfg_attr(feature = "serde", derive(serde::Serialize))]
struct Attrs {
is_excluded: bool,
attributes: Vec<gix::attrs::Assignment>,
Expand Down Expand Up @@ -337,7 +331,7 @@ pub(crate) mod function {
flags: u32,
mode: u32,
path: std::borrow::Cow<'a, str>,
meta: Option<AttrsForSerde>,
meta: Option<Attrs>,
}

serde_json::to_writer(
Expand All @@ -354,14 +348,7 @@ pub(crate) mod function {
path.extend_from_slice(entry.path(index));
path.to_string().into()
},
meta: attrs.map(|attrs| AttrsForSerde {
is_excluded: attrs.is_excluded,
attributes: attrs
.attributes
.into_iter()
.map(|attr| attr.as_ref().to_string())
.collect(),
}),
meta: attrs,
},
)?;

Expand Down
4 changes: 2 additions & 2 deletions gix-attributes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ doctest = false

[features]
## Data structures implement `serde::Serialize` and `serde::Deserialize`.
serde = ["dep:serde", "bstr/serde", "gix-glob/serde"]
serde = ["dep:serde", "bstr/serde", "gix-glob/serde", "kstring/serde"]

[dependencies]
gix-path = { version = "^0.10.0", path = "../gix-path" }
Expand All @@ -24,7 +24,7 @@ gix-trace = { version = "^0.1.3", path = "../gix-trace" }

bstr = { version = "1.3.0", default-features = false, features = ["std", "unicode"]}
smallvec = "1.10.0"
byteyarn = "0.2.3"
kstring = "2.0.0"
unicode-bom = "2.0.2"
thiserror = "1.0.26"
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"]}
Expand Down
11 changes: 8 additions & 3 deletions gix-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![deny(missing_docs, rust_2018_idioms, unsafe_code)]

use byteyarn::{Yarn, YarnRef};
pub use gix_glob as glob;
use kstring::{KString, KStringRef};

mod assignment;
///
Expand All @@ -34,13 +34,15 @@ pub fn parse(bytes: &[u8]) -> parse::Lines<'_> {
///
/// Note that this doesn't contain the name.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum StateRef<'a> {
/// The attribute is listed, or has the special value 'true'
Set,
/// The attribute has the special value 'false', or was prefixed with a `-` sign.
Unset,
/// The attribute is set to the given value, which followed the `=` sign.
/// Note that values can be empty.
#[cfg_attr(feature = "serde", serde(borrow))]
Value(state::ValueRef<'a>),
/// The attribute isn't mentioned with a given path or is explicitly set to `Unspecified` using the `!` sign.
Unspecified,
Expand All @@ -50,6 +52,7 @@ pub enum StateRef<'a> {
///
/// Note that this doesn't contain the name.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum State {
/// The attribute is listed, or has the special value 'true'
Set,
Expand All @@ -64,14 +67,16 @@ pub enum State {

/// Represents a validated attribute name
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub struct Name(pub(crate) Yarn);
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Name(pub(crate) KString);

/// Holds a validated attribute name as a reference
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
pub struct NameRef<'a>(YarnRef<'a, str>);
pub struct NameRef<'a>(KStringRef<'a>);

/// Name an attribute and describe it's assigned state.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Assignment {
/// The validated name of the attribute.
pub name: Name,
Expand Down
10 changes: 3 additions & 7 deletions gix-attributes/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use bstr::{BStr, BString, ByteSlice};
use byteyarn::YarnRef;
use kstring::KStringRef;

use crate::{Name, NameRef};

impl<'a> NameRef<'a> {
/// Turn this ref into its owned counterpart.
pub fn to_owned(self) -> Name {
Name(
self.0
.immortalize()
.map_or_else(|| self.0.to_boxed_str().into(), YarnRef::to_box),
)
Name(self.0.into())
}

/// Return the inner `str`.
Expand Down Expand Up @@ -39,7 +35,7 @@ impl<'a> TryFrom<&'a BStr> for NameRef<'a> {
}

attr_valid(attr)
.then(|| NameRef(YarnRef::from(attr.to_str().expect("no illformed utf8"))))
.then(|| NameRef(KStringRef::from_ref(attr.to_str().expect("no illformed utf8"))))
.ok_or_else(|| Error { attribute: attr.into() })
}
}
Expand Down
5 changes: 3 additions & 2 deletions gix-attributes/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::borrow::Cow;

use bstr::{BStr, ByteSlice};
use byteyarn::YarnRef;
use kstring::KStringRef;

use crate::{name, AssignmentRef, Name, NameRef, StateRef};

/// The kind of attribute that was parsed.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Kind {
/// A pattern to match paths against
Pattern(gix_glob::Pattern),
Expand Down Expand Up @@ -75,7 +76,7 @@ fn check_attr(attr: &BStr) -> Result<NameRef<'_>, name::Error> {
}

attr_valid(attr)
.then(|| NameRef(YarnRef::from(attr.to_str().expect("no illformed utf8"))))
.then(|| NameRef(KStringRef::from_ref(attr.to_str().expect("no illformed utf8"))))
.ok_or_else(|| name::Error { attribute: attr.into() })
}

Expand Down
6 changes: 3 additions & 3 deletions gix-attributes/src/search/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use byteyarn::Yarn;
use kstring::KString;
use smallvec::SmallVec;

use crate::{Assignment, AssignmentRef};
Expand Down Expand Up @@ -99,7 +99,7 @@ pub struct Outcome {
/// A stack of attributes to use for processing attributes of matched patterns and for resolving their macros.
attrs_stack: SmallVec<[(AttributeId, Assignment, Option<AttributeId>); 8]>,
/// A set of attributes we should limit ourselves to, or empty if we should fill in all attributes, made of
selected: SmallVec<[(Yarn, Option<AttributeId>); AVERAGE_NUM_ATTRS]>,
selected: SmallVec<[(KString, Option<AttributeId>); AVERAGE_NUM_ATTRS]>,
/// storage for all patterns we have matched so far (in order to avoid referencing them, we copy them, but only once).
patterns: RefMap<gix_glob::Pattern>,
/// storage for all assignments we have matched so far (in order to avoid referencing them, we copy them, but only once).
Expand Down Expand Up @@ -135,7 +135,7 @@ pub struct MetadataCollection {
/// A mapping of an attribute or macro name to its order, that is the time when it was *first* seen.
///
/// This is the inverse of the order attributes are searched.
name_to_meta: HashMap<Yarn, Metadata>,
name_to_meta: HashMap<KString, Metadata>,
}

/// Metadata associated with an attribute or macro name.
Expand Down
14 changes: 7 additions & 7 deletions gix-attributes/src/search/outcome.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bstr::{BString, ByteSlice};
use byteyarn::YarnBox;
use gix_glob::Pattern;
use kstring::{KString, KStringRef};

use crate::{
search::{
Expand Down Expand Up @@ -44,23 +44,23 @@ impl Outcome {
pub fn initialize_with_selection<'a>(
&mut self,
collection: &MetadataCollection,
attribute_names: impl IntoIterator<Item = impl Into<&'a str>>,
attribute_names: impl IntoIterator<Item = impl Into<KStringRef<'a>>>,
) {
self.initialize_with_selection_inner(collection, &mut attribute_names.into_iter().map(Into::into))
}

fn initialize_with_selection_inner(
&mut self,
collection: &MetadataCollection,
attribute_names: &mut dyn Iterator<Item = &str>,
attribute_names: &mut dyn Iterator<Item = KStringRef<'_>>,
) {
self.initialize(collection);

self.selected.clear();
self.selected.extend(attribute_names.map(|name| {
(
YarnBox::new(name).immortalize(),
collection.name_to_meta.get(name).map(|meta| meta.id),
name.to_owned(),
collection.name_to_meta.get(name.as_str()).map(|meta| meta.id),
)
}));
self.reset_remaining();
Expand Down Expand Up @@ -314,7 +314,7 @@ impl MetadataCollection {
None => {
let order = AttributeId(self.name_to_meta.len());
self.name_to_meta.insert(
YarnBox::new(name).immortalize(),
KString::from_ref(name),
Metadata {
id: order,
macro_attributes: Default::default(),
Expand All @@ -334,7 +334,7 @@ impl MetadataCollection {
Some(meta) => meta.id,
None => {
let order = AttributeId(self.name_to_meta.len());
self.name_to_meta.insert(YarnBox::new(name).immortalize(), order.into());
self.name_to_meta.insert(KString::from_ref(name), order.into());
order
}
}
Expand Down
25 changes: 15 additions & 10 deletions gix-attributes/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
use bstr::{BStr, ByteSlice};
use byteyarn::{ByteYarn, YarnBox, YarnRef};
use kstring::{KString, KStringRef};

use crate::{State, StateRef};

/// A container to encapsulate a tightly packed and typically unallocated byte value that isn't necessarily UTF8 encoded.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub struct Value(ByteYarn);
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Value(KString);

/// A reference container to encapsulate a tightly packed and typically unallocated byte value that isn't necessarily UTF8 encoded.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
pub struct ValueRef<'a>(YarnRef<'a, [u8]>);
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ValueRef<'a>(#[cfg_attr(feature = "serde", serde(borrow))] KStringRef<'a>);

/// Lifecycle
impl<'a> ValueRef<'a> {
/// Keep `input` as our value.
pub fn from_bytes(input: &'a [u8]) -> Self {
Self(YarnRef::from(input))
Self(KStringRef::from_ref(
// SAFETY: our API makes accessing that value as `str` impossible, so illformed UTF8 is never exposed as such.
#[allow(unsafe_code)]
unsafe {
std::str::from_utf8_unchecked(input)
},
))
}
}

Expand All @@ -34,22 +42,19 @@ impl ValueRef<'_> {

impl<'a> From<&'a str> for ValueRef<'a> {
fn from(v: &'a str) -> Self {
ValueRef(v.as_bytes().into())
ValueRef(v.into())
}
}

impl<'a> From<ValueRef<'a>> for Value {
fn from(v: ValueRef<'a>) -> Self {
Value(
v.0.immortalize()
.map_or_else(|| v.0.to_boxed_bytes().into(), YarnRef::to_box),
)
Value(v.0.into())
}
}

impl From<&str> for Value {
fn from(v: &str) -> Self {
Value(YarnBox::new(v).immortalize().into())
Value(KString::from_ref(v))
}
}

Expand Down
2 changes: 1 addition & 1 deletion gix-attributes/tests/search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ fn given_attributes_are_made_available_in_given_order() -> crate::Result {
fn size_of_outcome() {
assert_eq!(
std::mem::size_of::<Outcome>(),
752,
904,
"it's quite big, shouldn't change without us noticing"
)
}
Expand Down