Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit committed Jan 7, 2025
1 parent b58a464 commit cb41a14
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 58 deletions.
45 changes: 21 additions & 24 deletions core/webidl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,52 @@ pub struct WebIdlError {
pub kind: WebIdlErrorKind,
}

type DynContext<'a> = dyn Fn() -> Cow<'static, str> + 'a;
type DynContextFn<'a> = dyn Fn() -> Cow<'static, str> + 'a;

enum MyCow<'a> {
Borrowed(&'a DynContext<'a>),
Owned(Box<DynContext<'a>>),
enum ContextFnInner<'a> {
Borrowed(&'a DynContextFn<'a>),
Owned(Box<DynContextFn<'a>>),
}

pub struct ContextFn<'a>(MyCow<'a>);
/// A function that returns a context string for an error.
///
/// When possible, prefer to use `ContextFn::new_borrowed` when creating a new context function
/// to avoid unnecessary allocations.
///
/// To pass a borrow of the context function, use `ContextFn::borrowed`.
pub struct ContextFn<'a>(ContextFnInner<'a>);

impl<'a, T> From<T> for ContextFn<'a>
where
T: Fn() -> Cow<'static, str> + 'a,
{
fn from(f: T) -> Self {
Self(MyCow::Owned(Box::new(f)))
Self(ContextFnInner::Owned(Box::new(f)))
}
}

impl<'a> ContextFn<'a> {
pub fn call(&self) -> Cow<'static, str> {
match &self.0 {
MyCow::Borrowed(b) => b(),
MyCow::Owned(b) => b(),
ContextFnInner::Borrowed(b) => b(),
ContextFnInner::Owned(b) => b(),
}
}

pub fn new(f: impl Fn() -> Cow<'static, str> + 'a) -> Self {
Self(MyCow::Owned(Box::new(f)))
Self(ContextFnInner::Owned(Box::new(f)))
}

pub fn new_borrowed(b: &'a DynContext<'a>) -> Self {
Self(MyCow::Borrowed(b))
}
}

impl<'a> AsRef<DynContext<'a>> for ContextFn<'a> {
fn as_ref(&self) -> &DynContext<'a> {
match &self.0 {
MyCow::Borrowed(b) => *b,
MyCow::Owned(b) => b.as_ref(),
}
pub fn new_borrowed(b: &'a DynContextFn<'a>) -> Self {
Self(ContextFnInner::Borrowed(b))
}
}

impl<'a> ContextFn<'a> {
pub fn borrowed(&'a self) -> ContextFn<'a> {
match self {
Self(MyCow::Borrowed(b)) => Self(MyCow::Borrowed(*b)),
Self(MyCow::Owned(b)) => Self(MyCow::Borrowed(&**b)),
Self(ContextFnInner::Borrowed(b)) => Self(ContextFnInner::Borrowed(*b)),
Self(ContextFnInner::Owned(b)) => Self(ContextFnInner::Borrowed(&**b)),
}
}
}
Expand Down Expand Up @@ -371,8 +368,8 @@ impl<'a, T: WebIdlConverter<'a>> WebIdlConverter<'a> for Vec<T> {
scope,
iter_val,
prefix.clone(),
ContextFn::from(|| {
format!("{}, index {}", (context.as_ref())(), out.len()).into()
ContextFn::new_borrowed(&|| {
format!("{}, index {}", context.call(), out.len()).into()
}),
options,
)?);
Expand Down
2 changes: 1 addition & 1 deletion ops/op2/dispatch_slow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ pub fn from_arg(
&mut #scope,
#arg_ident,
#prefix.into(),
deno_core::webidl::ContextFn::from(|| std::borrow::Cow::Borrowed(#context)),
deno_core::webidl::ContextFn::new_borrowed(&|| std::borrow::Cow::Borrowed(#context)),
&#options,
) {
Ok(t) => t,
Expand Down
8 changes: 6 additions & 2 deletions ops/op2/test_cases/sync/webidl.out

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

4 changes: 2 additions & 2 deletions ops/webidl/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ pub fn get_body(
let Ok(str) = __value.try_cast::<::deno_core::v8::String>() else {
return Err(::deno_core::webidl::WebIdlError::new(
__prefix,
&__context,
__context,
::deno_core::webidl::WebIdlErrorKind::ConvertToConverterType("enum"),
));
};

match str.to_rust_string_lossy(__scope).as_str() {
#(#variants),*,
s => Err(::deno_core::webidl::WebIdlError::new(__prefix, &__context, ::deno_core::webidl::WebIdlErrorKind::InvalidEnumVariant { converter: #ident_string, variant: s.to_string() }))
s => Err(::deno_core::webidl::WebIdlError::new(__prefix, __context, ::deno_core::webidl::WebIdlErrorKind::InvalidEnumVariant { converter: #ident_string, variant: s.to_string() }))
}
})
}
Expand Down
49 changes: 28 additions & 21 deletions ops/webidl/test_cases/dict.out

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

13 changes: 5 additions & 8 deletions ops/webidl/test_cases/enum.out

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

0 comments on commit cb41a14

Please sign in to comment.