Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
remove value_bag from public API
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Mar 25, 2024
1 parent cf09743 commit 356ca03
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 97 deletions.
2 changes: 1 addition & 1 deletion core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<'a, P: Props> fmt::Debug for Event<'a, P> {
let mut f = f.debug_struct("");

self.0.for_each(|k, v| {
f.field(k.as_str(), &v);
f.field(k.get(), &v);

ControlFlow::Continue(())
});
Expand Down
36 changes: 27 additions & 9 deletions core/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::fmt;
use core::{fmt, str};

use crate::{
str::Str,
Expand Down Expand Up @@ -34,30 +34,36 @@ impl<'a> FromValue<'a> for Path<'a> {

impl Path<'static> {
pub const fn new(path: &'static str) -> Self {
Path(Str::new(path))
Path::new_str(Str::new(path))
}
}

impl<'a> Path<'a> {
pub const fn new_ref(path: &'a str) -> Self {
Path(Str::new_ref(path))
Self::new_str(Str::new_ref(path))
}

pub const fn new_str(path: Str<'a>) -> Self {
Path(path)
}

pub fn by_ref<'b>(&'b self) -> Path<'b> {
Path(self.0.by_ref())
}

pub fn segments(&self) -> impl Iterator<Item = &str> {
self.0.as_str().split("::")
pub fn segments(&self) -> Segments {
Segments {
inner: self.0.get().split("::"),
}
}

pub fn as_str(&self) -> Option<&str> {
Some(self.0.as_str())
pub fn as_str(&self) -> Option<&Str<'a>> {
Some(&self.0)
}

pub fn is_child_of<'b>(&self, other: &Path<'b>) -> bool {
let child = self.0.as_str();
let parent = other.0.as_str();
let child = self.0.get();
let parent = other.0.get();

if child.len() >= parent.len() && child.is_char_boundary(parent.len()) {
let (child_prefix, child_suffix) = child.split_at(parent.len());
Expand All @@ -69,6 +75,18 @@ impl<'a> Path<'a> {
}
}

pub struct Segments<'a> {
inner: str::Split<'a, &'static str>,
}

impl<'a> Iterator for Segments<'a> {
type Item = Str<'a>;

fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(Str::new_ref)
}
}

impl<'a> Eq for Path<'a> {}

impl<'a, 'b> PartialEq<Path<'b>> for Path<'a> {
Expand Down
40 changes: 20 additions & 20 deletions core/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub struct Str<'k> {

impl<'k> fmt::Debug for Str<'k> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)
fmt::Debug::fmt(self.get(), f)
}
}

impl<'k> fmt::Display for Str<'k> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
fmt::Display::fmt(self.get(), f)
}
}

Expand Down Expand Up @@ -100,74 +100,74 @@ impl<'k> Str<'k> {
}
}

pub const fn as_str(&self) -> &str {
pub const fn get(&self) -> &str {
unsafe { &(*self.value) }
}

pub const fn as_static_str(&self) -> Option<&'static str> {
pub const fn get_static(&self) -> Option<&'static str> {
self.value_static
}
}

impl<'a> hash::Hash for Str<'a> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state)
self.get().hash(state)
}
}

impl<'a, 'b> PartialEq<Str<'b>> for Str<'a> {
fn eq(&self, other: &Str<'b>) -> bool {
self.as_str() == other.as_str()
self.get() == other.get()
}
}

impl<'a> Eq for Str<'a> {}

impl<'a> PartialEq<str> for Str<'a> {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
self.get() == other
}
}

impl<'a> PartialEq<Str<'a>> for str {
fn eq(&self, other: &Str<'a>) -> bool {
self == other.as_str()
self == other.get()
}
}

impl<'a, 'b> PartialEq<&'b str> for Str<'a> {
fn eq(&self, other: &&'b str) -> bool {
self.as_str() == *other
self.get() == *other
}
}

impl<'a, 'b> PartialEq<Str<'b>> for &'a str {
fn eq(&self, other: &Str<'b>) -> bool {
*self == other.as_str()
*self == other.get()
}
}

impl<'a, 'b> PartialOrd<Str<'b>> for Str<'a> {
fn partial_cmp(&self, other: &Str<'b>) -> Option<core::cmp::Ordering> {
self.as_str().partial_cmp(other.as_str())
self.get().partial_cmp(other.get())
}
}

impl<'a> Ord for Str<'a> {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.as_str().cmp(other.as_str())
self.get().cmp(other.get())
}
}

impl<'k> Borrow<str> for Str<'k> {
fn borrow(&self) -> &str {
self.as_str()
self.get()
}
}

impl<'k> AsRef<str> for Str<'k> {
fn as_ref(&self) -> &str {
self.as_str()
self.get()
}
}

Expand All @@ -179,7 +179,7 @@ impl<'a> From<&'a str> for Str<'a> {

impl<'k> ToValue for Str<'k> {
fn to_value(&self) -> Value {
Value::from(self.as_str())
Value::from(self.get())
}
}

Expand Down Expand Up @@ -230,18 +230,18 @@ impl<'k> sval::Value for Str<'k> {
#[cfg(feature = "sval")]
impl<'k> sval_ref::ValueRef<'k> for Str<'k> {
fn stream_ref<S: sval::Stream<'k> + ?Sized>(&self, stream: &mut S) -> sval::Result {
if let Some(k) = self.as_static_str() {
if let Some(k) = self.get_static() {
stream.value(k)
} else {
stream.value_computed(self.as_str())
stream.value_computed(self.get())
}
}
}

#[cfg(feature = "serde")]
impl<'k> serde::Serialize for Str<'k> {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_str().serialize(serializer)
self.get().serialize(serializer)
}
}

Expand Down Expand Up @@ -275,14 +275,14 @@ mod alloc_support {
pub fn to_cow(&self) -> Cow<'static, str> {
match self.value_static {
Some(key) => Cow::Borrowed(key),
None => Cow::Owned(self.as_str().to_owned()),
None => Cow::Owned(self.get().to_owned()),
}
}

pub fn to_owned(&self) -> Str<'static> {
match self.value_static {
Some(key) => Str::new(key),
None => Str::new_owned(self.as_str()),
None => Str::new_owned(self.get()),
}
}
}
Expand Down
35 changes: 12 additions & 23 deletions core/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum TemplateKind<'a> {
}

impl<'a> TemplateKind<'a> {
fn parts(&self) -> &[Part] {
fn parts(&self) -> &[Part<'a>] {
match self {
TemplateKind::Literal(ref parts) => parts,
TemplateKind::Parts(parts) => parts,
Expand Down Expand Up @@ -78,20 +78,13 @@ impl<'a> Template<'a> {
}
}

pub fn as_str(&self) -> Option<&str> {
pub fn as_str(&'_ self) -> Option<&'_ Str<'a>> {
match self.0.parts() {
[part] => part.as_str(),
_ => None,
}
}

pub fn as_static_str(&self) -> Option<&'static str> {
match self.0.parts() {
[Part(PartKind::Text { value, .. })] => value.as_static_str(),
_ => None,
}
}

pub fn render<'b, P>(&'b self, props: P) -> Render<'b, P> {
Render {
tpl: self.by_ref(),
Expand All @@ -103,7 +96,7 @@ impl<'a> Template<'a> {
impl<'a> ToValue for Template<'a> {
fn to_value(&self) -> Value {
if let Some(tpl) = self.as_str() {
Value::from(tpl)
Value::from_any(tpl)
} else {
Value::from_display(self)
}
Expand Down Expand Up @@ -131,8 +124,8 @@ impl<'a, 'b> PartialEq<Template<'b>> for Template<'a> {

match (&ap.0, &bp.0) {
(PartKind::Text { value: ref a }, PartKind::Text { value: ref b }) => {
let a = a.as_str();
let b = b.as_str();
let a = a.get();
let b = b.get();

let at = &a[ati..];
let bt = &b[bti..];
Expand Down Expand Up @@ -181,7 +174,7 @@ impl<'a, 'b> PartialEq<Template<'b>> for Template<'a> {
return false;
};

if !value.as_str().is_empty() {
if !value.get().is_empty() {
return false;
}
}
Expand All @@ -206,13 +199,9 @@ impl<'a, P> Render<'a, P> {
}
}

pub fn as_str(&self) -> Option<&str> {
pub fn as_str(&'_ self) -> Option<&'_ Str<'a>> {
self.tpl.as_str()
}

pub fn as_static_str(&self) -> Option<&'static str> {
self.tpl.as_static_str()
}
}

impl<'a, P: Props> Render<'a, P> {
Expand All @@ -228,7 +217,7 @@ impl<'a, P: Props> Render<'a, P> {
impl<'a, P: Props> ToValue for Render<'a, P> {
fn to_value(&self) -> Value {
if let Some(tpl) = self.as_str() {
Value::from(tpl)
Value::from_any(tpl)
} else {
Value::from_display(self)
}
Expand Down Expand Up @@ -407,9 +396,9 @@ impl<'a> Part<'a> {
})
}

pub const fn as_str(&self) -> Option<&str> {
pub const fn as_str(&'_ self) -> Option<&'_ Str<'a>> {
match self.0 {
PartKind::Text { ref value, .. } => Some(value.as_str()),
PartKind::Text { ref value, .. } => Some(value),
_ => None,
}
}
Expand Down Expand Up @@ -444,13 +433,13 @@ impl<'a> Part<'a> {

fn write(&self, mut writer: impl Write, props: impl Props) -> fmt::Result {
match self.0 {
PartKind::Text { ref value, .. } => writer.write_text(value.as_str()),
PartKind::Text { ref value, .. } => writer.write_text(value.get()),
PartKind::Hole {
ref label,
ref formatter,
..
} => {
let label = label.as_str();
let label = label.get();

if let Some(value) = props.get(label) {
if let Some(formatter) = formatter {
Expand Down
Loading

0 comments on commit 356ca03

Please sign in to comment.