Skip to content

Commit

Permalink
Loosened Lifetime Restrictions
Browse files Browse the repository at this point in the history
Fixed Clippy Warnings
  • Loading branch information
Redfire75369 committed Oct 20, 2023
1 parent 289b656 commit 09bc646
Show file tree
Hide file tree
Showing 31 changed files with 122 additions and 229 deletions.
5 changes: 1 addition & 4 deletions ion-proc/src/class/automatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ pub(crate) fn from_value(ion: &TokenStream, class_ident: &Ident) -> ItemImpl {
parse2(quote!(
impl<'cx> #ion::conversions::FromValue<'cx> for #class_ident {
type Config = ();
fn from_value<'v>(cx: &'cx #ion::Context, value: &#ion::Value<'v>, _: bool, _: ()) -> #ion::Result<#class_ident>
where
'cx: 'v
{
fn from_value<'v>(cx: &'cx #ion::Context, value: &#ion::Value<'v>, _: bool, _: ()) -> #ion::Result<#class_ident> {
#ion::class::class_from_value(cx, value)
}
}
Expand Down
14 changes: 6 additions & 8 deletions ion-proc/src/function/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl ThisParameter {
pub(crate) fn to_statement(&self, ion: &TokenStream, is_class: bool) -> Result<Stmt> {
let ThisParameter { pat, ty, kind } = self;
let self_ = parse_quote!(self_);
let pat = if is_class && &**pat == &parse_quote!(self) { &self_ } else { pat };
let pat = if is_class && **pat == parse_quote!(self) { &self_ } else { pat };
let mut ty = ty.clone();
visit_type_mut(&mut LifetimeRemover, &mut ty);

Expand Down Expand Up @@ -393,13 +393,11 @@ pub(crate) fn parse_this(pat: Box<Pat>, ty: Box<Type>, is_class: bool, span: Spa
let lt = reference.lifetime;
let mutability = reference.mutability;
match *reference.elem {
Type::Path(ty) if type_ends_with(&ty, "Object") => {
return Ok(ThisParameter {
pat,
ty: Box::new(Type::Path(ty)),
kind: ThisKind::Object(lt, mutability),
});
}
Type::Path(ty) if type_ends_with(&ty, "Object") => Ok(ThisParameter {
pat,
ty: Box::new(Type::Path(ty)),
kind: ThisKind::Object(lt, mutability),
}),
ty => Ok(ThisParameter {
pat,
ty: Box::new(ty),
Expand Down
6 changes: 1 addition & 5 deletions ion-proc/src/function/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

use proc_macro2::{Ident, TokenStream};
use syn::{FnArg, GenericParam, ItemFn, parse2, PathArguments, Result, ReturnType, Type, WhereClause};
use syn::{FnArg, GenericParam, ItemFn, parse2, PathArguments, Result, ReturnType, Type};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;

Expand All @@ -32,7 +32,6 @@ pub(crate) fn impl_wrapper_fn(
let argument_checker = argument_checker(ion, &function.sig.ident, parameters.nargs.0);

let wrapper_generics: [GenericParam; 2] = [parse_quote!('cx), parse_quote!('a)];
let wrapper_where: WhereClause = parse_quote!(where 'cx: 'a);
let mut wrapper_args: Vec<FnArg> = vec![
parse_quote!(__cx: &'cx #ion::Context),
parse_quote!(__args: &'a mut #ion::Arguments<'_, 'cx>),
Expand Down Expand Up @@ -117,7 +116,6 @@ pub(crate) fn impl_wrapper_fn(
function.sig.ident = format_ident!("wrapper", span = function.sig.ident.span());
function.sig.inputs = Punctuated::from_iter(wrapper_args);
function.sig.generics.params = Punctuated::from_iter(wrapper_generics);
function.sig.generics.where_clause = Some(wrapper_where);
function.sig.output = parse_quote!(-> #wrapper_output);
function.sig.asyncness = None;
function.sig.unsafety = Some(<Token![unsafe]>::default());
Expand All @@ -143,7 +141,6 @@ pub(crate) fn impl_async_wrapper_fn(
let argument_checker = argument_checker(ion, &function.sig.ident, parameters.nargs.0);

let wrapper_generics: [GenericParam; 2] = [parse_quote!('cx), parse_quote!('a)];
let wrapper_where: WhereClause = parse_quote!(where 'cx: 'a);
let wrapper_args: Vec<FnArg> = vec![
parse_quote!(__cx: &'cx #ion::Context),
parse_quote!(__args: &'a mut #ion::Arguments<'_, 'cx>),
Expand Down Expand Up @@ -228,7 +225,6 @@ pub(crate) fn impl_async_wrapper_fn(
function.sig.ident = format_ident!("wrapper", span = function.sig.ident.span());
function.sig.inputs = Punctuated::from_iter(wrapper_args);
function.sig.generics.params = Punctuated::from_iter(wrapper_generics);
function.sig.generics.where_clause = Some(wrapper_where);
function.sig.output = parse_quote!(-> #wrapper_output);
function.sig.asyncness = None;
function.sig.unsafety = Some(<Token![unsafe]>::default());
Expand Down
4 changes: 1 addition & 3 deletions ion-proc/src/value/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ pub(crate) fn impl_from_value(mut input: DeriveInput) -> Result<ItemImpl> {
impl #impl_generics #ion::conversions::FromValue<'cx> for #name #ty_generics #where_clause {
type Config = ();

fn from_value<'v>(cx: &'cx #ion::Context, value: &#ion::Value<'v>, strict: bool, _: ())
-> #ion::Result<Self> where 'cx: 'v
{
fn from_value<'v>(cx: &'cx #ion::Context, value: &#ion::Value<'v>, strict: bool, _: ()) -> #ion::Result<Self> {
#object
#body
}
Expand Down
2 changes: 1 addition & 1 deletion ion/examples/macros/from_value/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ pub struct Complex<'cx> {
pub parsed: Arc<AtomicU64>,
}

fn parse_as_atomic_arc<'cx: 'v, 'v>(cx: &'cx Context, value: Value<'v>) -> Result<Arc<AtomicU64>> {
fn parse_as_atomic_arc(cx: &Context, value: Value) -> Result<Arc<AtomicU64>> {
u64::from_value(cx, &value, true, ConversionBehavior::Default).map(|num| Arc::new(AtomicU64::new(num)))
}
4 changes: 2 additions & 2 deletions ion/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub trait ClassDefinition {
}
}

fn set_private(object: *mut JSObject, native: Self)
unsafe fn set_private(object: *mut JSObject, native: Self)
where
Self: Sized,
{
Expand All @@ -172,7 +172,7 @@ pub trait ClassDefinition {
}

/// Converts an instance of a native class into its native value, by cloning it.
pub fn class_from_value<'cx: 'v, 'v, T: ClassDefinition + Clone>(cx: &'cx Context, value: &Value<'v>) -> Result<T> {
pub fn class_from_value<T: ClassDefinition + Clone>(cx: &Context, value: &Value) -> Result<T> {
let object = Object::from_value(cx, value, true, ()).unwrap();
if T::instance_of(cx, &object, None) {
Ok(T::get_private(&object).clone())
Expand Down
109 changes: 22 additions & 87 deletions ion/src/conversions/value/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@ pub trait FromValue<'cx>: Sized {
/// Converts `value` to the desired type.
/// `strict` and `config` determine the strictness of the conversion and specify additional conversion constraints respectively.
/// Returns [Err] with the [error](Error) if conversion fails.
fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, config: Self::Config) -> Result<Self>
where
'cx: 'v;
fn from_value(cx: &'cx Context, value: &Value, strict: bool, config: Self::Config) -> Result<Self>;
}

impl<'cx> FromValue<'cx> for bool {
type Config = ();

fn from_value<'v>(_: &'cx Context, value: &Value<'v>, strict: bool, _: ()) -> Result<bool>
where
'cx: 'v,
{
fn from_value(_: &'cx Context, value: &Value, strict: bool, _: ()) -> Result<bool> {
let value = value.handle();
if value.is_boolean() {
return Ok(value.to_boolean());
Expand All @@ -56,10 +51,7 @@ macro_rules! impl_from_value_for_integer {
impl<'cx> FromValue<'cx> for $ty {
type Config = ConversionBehavior;

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, config: ConversionBehavior) -> Result<$ty>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, config: ConversionBehavior) -> Result<$ty> {
let value = value.handle();
if strict && !value.is_number() {
return Err(Error::new("Expected Number in Strict Conversion", ErrorKind::Type));
Expand Down Expand Up @@ -88,21 +80,15 @@ impl_from_value_for_integer!(i64);
impl<'cx> FromValue<'cx> for f32 {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, _: ()) -> Result<f32>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, _: ()) -> Result<f32> {
f64::from_value(cx, value, strict, ()).map(|float| float as f32)
}
}

impl<'cx> FromValue<'cx> for f64 {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, _: ()) -> Result<f64>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, _: ()) -> Result<f64> {
let value = value.handle();
if strict && !value.is_number() {
return Err(Error::new("Expected Number in Strict Conversion", ErrorKind::Type));
Expand All @@ -116,10 +102,7 @@ impl<'cx> FromValue<'cx> for f64 {
impl<'cx> FromValue<'cx> for *mut JSString {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, _: ()) -> Result<*mut JSString>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, _: ()) -> Result<*mut JSString> {
let value = value.handle();
if strict && !value.is_string() {
return Err(Error::new("Expected String in Strict Conversion", ErrorKind::Type));
Expand All @@ -131,32 +114,23 @@ impl<'cx> FromValue<'cx> for *mut JSString {
impl<'cx> FromValue<'cx> for String<'cx> {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, config: ()) -> Result<String<'cx>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, config: ()) -> Result<String<'cx>> {
<*mut JSString>::from_value(cx, value, strict, config).map(|str| String::from(cx.root_string(str)))
}
}

impl<'cx> FromValue<'cx> for RustString {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, config: ()) -> Result<RustString>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, config: ()) -> Result<RustString> {
String::from_value(cx, value, strict, config).map(|s| s.to_owned(cx))
}
}

impl<'cx> FromValue<'cx> for *mut JSObject {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<*mut JSObject>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<*mut JSObject> {
let value = value.handle();
if !value.is_object() {
return Err(Error::new("Expected Object", ErrorKind::Type));
Expand All @@ -173,10 +147,7 @@ impl<'cx> FromValue<'cx> for *mut JSObject {
impl<'cx> FromValue<'cx> for Object<'cx> {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<Object<'cx>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<Object<'cx>> {
if !value.handle().is_object() {
return Err(Error::new("Expected Object", ErrorKind::Type));
}
Expand All @@ -192,10 +163,7 @@ impl<'cx> FromValue<'cx> for Object<'cx> {
impl<'cx> FromValue<'cx> for Array<'cx> {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<Array<'cx>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<Array<'cx>> {
if !value.handle().is_object() {
return Err(Error::new("Expected Array", ErrorKind::Type));
}
Expand All @@ -215,10 +183,7 @@ impl<'cx> FromValue<'cx> for Array<'cx> {
impl<'cx> FromValue<'cx> for Date<'cx> {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<Date<'cx>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<Date<'cx>> {
if !value.handle().is_object() {
return Err(Error::new("Expected Date", ErrorKind::Type));
}
Expand All @@ -238,10 +203,7 @@ impl<'cx> FromValue<'cx> for Date<'cx> {
impl<'cx> FromValue<'cx> for Promise<'cx> {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<Promise<'cx>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<Promise<'cx>> {
if !value.handle().is_object() {
return Err(Error::new("Expected Promise", ErrorKind::Type));
}
Expand All @@ -261,21 +223,15 @@ impl<'cx> FromValue<'cx> for Promise<'cx> {
impl<'cx> FromValue<'cx> for *mut JSFunction {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, config: ()) -> Result<*mut JSFunction>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, config: ()) -> Result<*mut JSFunction> {
Function::from_value(cx, value, strict, config).map(|f| f.get())
}
}

impl<'cx> FromValue<'cx> for Function<'cx> {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<Function<'cx>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<Function<'cx>> {
if !value.handle().is_object() {
return Err(Error::new("Expected Function", ErrorKind::Type));
}
Expand All @@ -295,10 +251,7 @@ impl<'cx> FromValue<'cx> for Function<'cx> {
impl<'cx> FromValue<'cx> for *mut JSSymbol {
type Config = ();

fn from_value<'v>(_: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<*mut JSSymbol>
where
'cx: 'v,
{
fn from_value(_: &'cx Context, value: &Value, _: bool, _: ()) -> Result<*mut JSSymbol> {
let value = value.handle();
if value.is_symbol() {
Ok(value.to_symbol())
Expand All @@ -311,21 +264,15 @@ impl<'cx> FromValue<'cx> for *mut JSSymbol {
impl<'cx> FromValue<'cx> for Symbol<'cx> {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, config: Self::Config) -> Result<Symbol<'cx>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, config: Self::Config) -> Result<Symbol<'cx>> {
<*mut JSSymbol>::from_value(cx, value, strict, config).map(|s| cx.root_symbol(s).into())
}
}

impl<'cx> FromValue<'cx> for JSVal {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<JSVal>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<JSVal> {
let value = value.handle();
unsafe {
AssertSameCompartment1(cx.as_ptr(), value.into());
Expand All @@ -337,10 +284,7 @@ impl<'cx> FromValue<'cx> for JSVal {
impl<'cx> FromValue<'cx> for Value<'cx> {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<Value<'cx>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<Value<'cx>> {
let value = value.handle();
unsafe {
AssertSameCompartment1(cx.as_ptr(), value.into());
Expand All @@ -352,10 +296,7 @@ impl<'cx> FromValue<'cx> for Value<'cx> {
impl<'cx, T: FromValue<'cx>> FromValue<'cx> for Option<T> {
type Config = T::Config;

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, config: T::Config) -> Result<Option<T>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, config: T::Config) -> Result<Option<T>> {
if value.handle().is_null_or_undefined() {
Ok(None)
} else {
Expand Down Expand Up @@ -383,10 +324,7 @@ where
type Config = T::Config;

// Adapted from [rust-mozjs](https://github.com/servo/rust-mozjs/blob/master/src/conversions.rs#L644-L707)
fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, strict: bool, config: T::Config) -> Result<Vec<T>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, strict: bool, config: T::Config) -> Result<Vec<T>> {
if !value.handle().is_object() {
return Err(Error::new("Expected Object", ErrorKind::Type));
}
Expand Down Expand Up @@ -433,10 +371,7 @@ where
impl<'cx, T: TypedArrayElement, S: JSObjectStorage> FromValue<'cx> for TypedArray<T, S> {
type Config = ();

fn from_value<'v>(cx: &'cx Context, value: &Value<'v>, _: bool, _: ()) -> Result<TypedArray<T, S>>
where
'cx: 'v,
{
fn from_value(cx: &'cx Context, value: &Value, _: bool, _: ()) -> Result<TypedArray<T, S>> {
let value = value.handle();
if value.is_object() {
let object = value.to_object();
Expand Down
2 changes: 1 addition & 1 deletion ion/src/format/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::format::Config;
use crate::format::object::format_plain_object;

/// Formats a [JavaScript Object](Object), along with the name of its constructor, as a string with the given [configuration](Config).
pub fn format_class_object<'cx: 'o, 'o>(cx: &'cx Context, cfg: Config, object: &Object<'o>) -> String {
pub fn format_class_object(cx: &Context, cfg: Config, object: &Object) -> String {
let class = unsafe { get_object_class(object.handle().get()) };
let name = unsafe { CStr::from_ptr((*class).name) }.to_str().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion ion/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub const INDENT: &str = " ";
pub const NEWLINE: &str = "\n";

/// Formats a [JavaScript Value](Value) as a string with the given [configuration](Config).
pub fn format_value<'cx: 'v, 'v>(cx: &'cx Context, cfg: Config, value: &Value<'v>) -> String {
pub fn format_value(cx: &Context, cfg: Config, value: &Value) -> String {
if !value.handle().is_object() {
format_primitive(cx, cfg, value)
} else {
Expand Down
Loading

0 comments on commit 09bc646

Please sign in to comment.