Skip to content

Commit

Permalink
Hide inner state
Browse files Browse the repository at this point in the history
  • Loading branch information
DanikVitek committed Sep 16, 2023
1 parent 4439777 commit 8350ae7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 73 deletions.
4 changes: 1 addition & 3 deletions leptos_dom/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub use dyn_child::*;
pub use each::*;
pub use errors::*;
pub use fragment::*;
#[cfg(any(debug_assertions, feature = "ssr"))]
use leptos_reactive::OcoInner;
use leptos_reactive::{untrack_with_diagnostics, Oco};
#[cfg(all(target_arch = "wasm32", feature = "web"))]
use once_cell::unsync::OnceCell;
Expand Down Expand Up @@ -235,7 +233,7 @@ impl ComponentRepr {

#[cfg(any(debug_assertions, feature = "ssr"))]
/// Returns the name of the component.
pub fn name(&self) -> Ref<'_, OcoInner<'static, str>> {
pub fn name(&self) -> Ref<'_, str> {
self.name.borrow()
}
}
Expand Down
2 changes: 1 addition & 1 deletion leptos_dom/src/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ impl View {
Some(
format!(
" {name}=\"{}\"",
html_escape::encode_double_quoted_attribute(&**value.borrow())
html_escape::encode_double_quoted_attribute(&*value.borrow())
)
.into(),
)
Expand Down
8 changes: 4 additions & 4 deletions leptos_dom/src/ssr_in_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl View {
Some(
format!(
" {name}=\"{}\"",
html_escape::encode_double_quoted_attribute(&**value.borrow())
html_escape::encode_double_quoted_attribute(&*value.borrow())
)
.into(),
)
Expand Down Expand Up @@ -414,7 +414,7 @@ impl View {
content
} else {
html_escape::encode_safe(
&**content.borrow(),
&*content.borrow(),
)
.to_string()
.into()
Expand All @@ -432,14 +432,14 @@ impl View {
format!(
"<!>{}",
html_escape::encode_safe(
&**content.borrow()
&*content.borrow()
)
)
.into(),
)
} else {
StreamChunk::Sync(html_escape::encode_safe(
&**content.borrow()
&*content.borrow()
).to_string().into())
},
);
Expand Down
73 changes: 8 additions & 65 deletions leptos_reactive/src/oco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub struct Oco<'a, T: ?Sized + ToOwned + 'a> {
inner: RefCell<OcoInner<'a, T>>,
}

/// The inner value of [`Oco`].
pub enum OcoInner<'a, T: ?Sized + ToOwned + 'a> {
#[doc(hidden)]
enum OcoInner<'a, T: ?Sized + ToOwned + 'a> {
/// A static reference to a value.
Borrowed(&'a T),
/// A reference counted pointer to a value.
Expand Down Expand Up @@ -96,11 +96,6 @@ impl<'a, T: ?Sized + ToOwned> Oco<'a, T> {
}
}

/// Returns the inner [`OcoInner`].
pub fn into_inner(self) -> OcoInner<'a, T> {
self.inner.into_inner()
}

/// Converts the value into an owned value.
pub fn into_owned(self) -> <T as ToOwned>::Owned {
match self.inner.into_inner() {
Expand All @@ -117,8 +112,8 @@ impl<'a, T: ?Sized + ToOwned> Oco<'a, T> {
/// let oco = Oco::<str>::from_borrowed("Hello");
/// assert_eq!(oco.borrow(), "Hello");
/// ```
pub fn borrow(&self) -> Ref<'_, OcoInner<'a, T>> {
self.inner.borrow()
pub fn borrow(&self) -> Ref<'_, T> {
Ref::map(self.inner.borrow(), Deref::deref)
}

/// Checks if the value is [`OcoInner::Borrowed`].
Expand All @@ -131,7 +126,7 @@ impl<'a, T: ?Sized + ToOwned> Oco<'a, T> {
/// assert!(!Oco::<str>::from_owned("Hello".to_string()).is_borrowed());
/// ```
pub fn is_borrowed(&self) -> bool {
self.borrow().is_borrowed()
self.inner.borrow().is_borrowed()
}

/// Checks if the value is [`OcoInner::Counted`].
Expand All @@ -144,7 +139,7 @@ impl<'a, T: ?Sized + ToOwned> Oco<'a, T> {
/// assert!(!Oco::<str>::from_owned("Hello".to_string()).is_counted());
/// ```
pub fn is_counted(&self) -> bool {
self.borrow().is_counted()
self.inner.borrow().is_counted()
}

/// Checks if the value is [`OcoInner::Owned`].
Expand All @@ -157,7 +152,7 @@ impl<'a, T: ?Sized + ToOwned> Oco<'a, T> {
/// assert!(!Oco::<str>::from_counted(Rc::from("Hello")).is_owned());
/// ```
pub fn is_owned(&self) -> bool {
self.borrow().is_owned()
self.inner.borrow().is_owned()
}
}

Expand Down Expand Up @@ -220,24 +215,6 @@ impl OcoInner<'_, str> {
}
}

impl OcoInner<'_, CStr> {
/// Returns a `&CStr` slice of this [`Oco`].
/// # Examples
/// ```
/// # use leptos_reactive::oco::OcoInner;
/// use std::ffi::CStr;
/// let oco = OcoInner::<CStr>::Borrowed(
/// CStr::from_bytes_with_nul(b"Hello\0").unwrap(),
/// );
/// let s: &CStr = oco.as_c_str();
/// assert_eq!(s, CStr::from_bytes_with_nul(b"Hello\0").unwrap());
/// ```
#[inline(always)]
pub fn as_c_str(&self) -> &CStr {
self
}
}

impl OcoInner<'_, OsStr> {
/// Returns a `&OsStr` slice of this [`Oco`].
/// # Examples
Expand All @@ -254,40 +231,6 @@ impl OcoInner<'_, OsStr> {
}
}

impl OcoInner<'_, Path> {
/// Returns a `&Path` slice of this [`Oco`].
/// # Examples
/// ```
/// # use leptos_reactive::oco::OcoInner;
/// use std::path::Path;
/// let oco = OcoInner::<Path>::Borrowed(Path::new("Hello"));
/// let s: &Path = oco.as_path();
/// assert_eq!(s, Path::new("Hello"));
/// ```
#[inline(always)]
pub fn as_path(&self) -> &Path {
self
}
}

impl<T> OcoInner<'_, [T]>
where
[T]: ToOwned,
{
/// Returns a `&[T]` slice of this [`Oco`].
/// # Examples
/// ```
/// # use leptos_reactive::oco::OcoInner;
/// let oco = OcoInner::<[i32]>::Borrowed(&[1, 2, 3]);
/// let s: &[i32] = oco.as_slice();
/// assert_eq!(s, &[1, 2, 3]);
/// ```
#[inline(always)]
pub fn as_slice(&self) -> &[T] {
self
}
}

impl Oco<'_, str> {
/// Checks if the value is an empty string.
/// # Examples
Expand Down Expand Up @@ -950,7 +893,7 @@ where
where
S: serde::Serializer,
{
self.borrow().serialize(serializer)
(&*self.borrow()).serialize(serializer)
}
}

Expand Down

0 comments on commit 8350ae7

Please sign in to comment.