Skip to content

Commit ad9ad60

Browse files
committed
v8: use clearer lifetime names
1 parent 036d03d commit ad9ad60

5 files changed

Lines changed: 109 additions & 102 deletions

File tree

crates/core/src/host/v8/de.rs

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ use std::borrow::{Borrow, Cow};
1414
use v8::{Array, Global, HandleScope, Local, Name, Object, Uint8Array, Value};
1515

1616
/// Deserializes from V8 values.
17-
pub(super) struct Deserializer<'a, 's> {
18-
common: DeserializerCommon<'a, 's>,
19-
input: Local<'s, Value>,
17+
pub(super) struct Deserializer<'this, 'scope> {
18+
common: DeserializerCommon<'this, 'scope>,
19+
input: Local<'scope, Value>,
2020
}
2121

22-
impl<'a, 's> Deserializer<'a, 's> {
22+
impl<'this, 'scope> Deserializer<'this, 'scope> {
2323
/// Creates a new deserializer from `input` in `scope`.
24-
pub fn new(scope: &'a mut HandleScope<'s>, input: Local<'_, Value>, key_cache: &'a mut KeyCache) -> Self {
24+
pub fn new(scope: &'this mut HandleScope<'scope>, input: Local<'_, Value>, key_cache: &'this mut KeyCache) -> Self {
2525
let input = Local::new(scope, input);
2626
let common = DeserializerCommon { scope, key_cache };
2727
Deserializer { input, common }
@@ -30,16 +30,16 @@ impl<'a, 's> Deserializer<'a, 's> {
3030

3131
/// Things shared between various [`Deserializer`]s.
3232
///
33-
/// The lifetime `'s` is that of the scope of values deserialized.
34-
struct DeserializerCommon<'a, 's> {
33+
/// The lifetime `'scope` is that of the scope of values deserialized.
34+
struct DeserializerCommon<'this, 'scope> {
3535
/// The scope of values to deserialize.
36-
scope: &'a mut HandleScope<'s>,
36+
scope: &'this mut HandleScope<'scope>,
3737
/// A cache for frequently used strings.
38-
key_cache: &'a mut KeyCache,
38+
key_cache: &'this mut KeyCache,
3939
}
4040

41-
impl<'s> DeserializerCommon<'_, 's> {
42-
fn reborrow(&mut self) -> DeserializerCommon<'_, 's> {
41+
impl<'scope> DeserializerCommon<'_, 'scope> {
42+
fn reborrow(&mut self) -> DeserializerCommon<'_, 'scope> {
4343
DeserializerCommon {
4444
scope: self.scope,
4545
key_cache: self.key_cache,
@@ -49,8 +49,8 @@ impl<'s> DeserializerCommon<'_, 's> {
4949

5050
/// The possible errors that [`Deserializer`] can produce.
5151
#[derive(Debug, From)]
52-
pub(super) enum Error<'s> {
53-
Value(Local<'s, Value>),
52+
pub(super) enum Error<'scope> {
53+
Value(Local<'scope, Value>),
5454
Exception(ExceptionThrown),
5555
Custom(String),
5656
}
@@ -77,22 +77,22 @@ pub(super) struct KeyCache {
7777

7878
impl KeyCache {
7979
/// Returns the `tag` property name.
80-
pub(super) fn tag<'s>(&mut self, scope: &mut HandleScope<'s>) -> Local<'s, v8::String> {
80+
pub(super) fn tag<'scope>(&mut self, scope: &mut HandleScope<'scope>) -> Local<'scope, v8::String> {
8181
Self::get_or_create_key(scope, &mut self.tag, "tag")
8282
}
8383

8484
/// Returns the `value` property name.
85-
pub(super) fn value<'s>(&mut self, scope: &mut HandleScope<'s>) -> Local<'s, v8::String> {
85+
pub(super) fn value<'scope>(&mut self, scope: &mut HandleScope<'scope>) -> Local<'scope, v8::String> {
8686
Self::get_or_create_key(scope, &mut self.value, "value")
8787
}
8888

8989
/// Returns an interned string corresponding to `string`
9090
/// and memoizes the creation on the v8 side.
91-
fn get_or_create_key<'s>(
92-
scope: &mut HandleScope<'s>,
91+
fn get_or_create_key<'scope>(
92+
scope: &mut HandleScope<'scope>,
9393
slot: &mut Option<Global<v8::String>>,
9494
string: &str,
95-
) -> Local<'s, v8::String> {
95+
) -> Local<'scope, v8::String> {
9696
if let Some(s) = &*slot {
9797
v8::Local::new(scope, s)
9898
} else {
@@ -104,24 +104,24 @@ impl KeyCache {
104104
}
105105

106106
// Creates an interned [`v8::String`].
107-
pub(super) fn v8_interned_string<'s>(scope: &mut HandleScope<'s>, field: &str) -> Local<'s, v8::String> {
107+
pub(super) fn v8_interned_string<'scope>(scope: &mut HandleScope<'scope>, field: &str) -> Local<'scope, v8::String> {
108108
// Internalized v8 strings are significantly faster than "normal" v8 strings
109109
// since v8 deduplicates re-used strings minimizing new allocations
110110
// see: https://github.com/v8/v8/blob/14ac92e02cc3db38131a57e75e2392529f405f2f/include/v8.h#L3165-L3171
111111
v8::String::new_from_utf8(scope, field.as_ref(), v8::NewStringType::Internalized).unwrap()
112112
}
113113

114-
/// Extracts a reference `&'s T` from an owned V8 [`Local<'s, T>`].
114+
/// Extracts a reference `&'scope T` from an owned V8 [`Local<'scope, T>`].
115115
///
116-
/// The lifetime `'s` is that of the [`HandleScope<'s>`].
116+
/// The lifetime `'scope` is that of the [`HandleScope<'scope>`].
117117
/// This ensures that the reference to `T` won't outlive the `HandleScope`.
118-
fn deref_local<'s, T>(local: Local<'s, T>) -> &'s T {
118+
fn deref_local<'scope, T>(local: Local<'scope, T>) -> &'scope T {
119119
let reference = local.borrow();
120-
// SAFETY: Lifetime extend `'0` to `'s`.
121-
// This is safe as the returned reference `&'s T`
122-
// will not outlive its `HandleScope<'s, _>`,
123-
// as both are tied to the lifetime `'s`.
124-
unsafe { core::mem::transmute::<&T, &'s T>(reference) }
120+
// SAFETY: Lifetime extend `'0` to `'scope`.
121+
// This is safe as the returned reference `&'scope T`
122+
// will not outlive its `HandleScope<'scope, _>`,
123+
// as both are tied to the lifetime `'scope`.
124+
unsafe { core::mem::transmute::<&T, &'scope T>(reference) }
125125
}
126126

127127
/// Deserializes a primitive via [`FromValue`].
@@ -133,8 +133,8 @@ macro_rules! deserialize_primitive {
133133
};
134134
}
135135

136-
impl<'de, 'a, 's: 'de> de::Deserializer<'de> for Deserializer<'a, 's> {
137-
type Error = Error<'s>;
136+
impl<'de, 'this, 'scope: 'de> de::Deserializer<'de> for Deserializer<'this, 'scope> {
137+
type Error = Error<'scope>;
138138

139139
// Deserialization of primitive types defers to `FromValue`.
140140
deserialize_primitive!(deserialize_bool, bool);
@@ -242,7 +242,7 @@ impl<'de, 'a, 's: 'de> de::Deserializer<'de> for Deserializer<'a, 's> {
242242
fn deserialize_bytes<V: SliceVisitor<'de, [u8]>>(self, visitor: V) -> Result<V::Output, Self::Error> {
243243
let arr = cast!(self.common.scope, self.input, Uint8Array, "`Uint8Array` for bytes")?;
244244
let storage: &'static mut [u8] = &mut [0; v8::TYPED_ARRAY_MAX_SIZE_IN_HEAP];
245-
let bytes: &'s [u8] = deref_local(arr).get_contents(storage);
245+
let bytes: &'scope [u8] = deref_local(arr).get_contents(storage);
246246
visitor.visit_borrowed(bytes)
247247
}
248248

@@ -258,27 +258,31 @@ impl<'de, 'a, 's: 'de> de::Deserializer<'de> for Deserializer<'a, 's> {
258258

259259
/// Provides access to the field names and values in a JS object
260260
/// under the assumption that it's a product.
261-
struct ProductAccess<'a, 's> {
262-
common: DeserializerCommon<'a, 's>,
261+
struct ProductAccess<'this, 'scope> {
262+
common: DeserializerCommon<'this, 'scope>,
263263
/// The input object being deserialized.
264-
object: Local<'s, Object>,
264+
object: Local<'scope, Object>,
265265
/// A field's value, to deserialize next in [`NamedProductAccess::get_field_value_seed`].
266-
next_value: Option<Local<'s, Value>>,
266+
next_value: Option<Local<'scope, Value>>,
267267
/// The index in the product to
268268
index: usize,
269269
}
270270

271271
/// Normalizes `field` into an interned `v8::String`.
272-
pub(super) fn intern_field_name<'s>(scope: &mut HandleScope<'s>, field: Option<&str>, index: usize) -> Local<'s, Name> {
272+
pub(super) fn intern_field_name<'scope>(
273+
scope: &mut HandleScope<'scope>,
274+
field: Option<&str>,
275+
index: usize,
276+
) -> Local<'scope, Name> {
273277
let field = match field {
274278
Some(field) => Cow::Borrowed(field),
275279
None => Cow::Owned(format!("{index}")),
276280
};
277281
v8_interned_string(scope, &field).into()
278282
}
279283

280-
impl<'de, 's: 'de> de::NamedProductAccess<'de> for ProductAccess<'_, 's> {
281-
type Error = Error<'s>;
284+
impl<'de, 'scope: 'de> de::NamedProductAccess<'de> for ProductAccess<'_, 'scope> {
285+
type Error = Error<'scope>;
282286

283287
fn get_field_ident<V: de::FieldNameVisitor<'de>>(&mut self, visitor: V) -> Result<Option<V::Output>, Self::Error> {
284288
let scope = &mut *self.common.scope;
@@ -330,17 +334,17 @@ impl<'de, 's: 'de> de::NamedProductAccess<'de> for ProductAccess<'_, 's> {
330334

331335
/// Used in `Deserializer::deserialize_sum` to translate a `tag` property of a JS object
332336
/// to a variant and to provide a deserializer for its value/payload.
333-
struct SumAccess<'a, 's> {
334-
common: DeserializerCommon<'a, 's>,
337+
struct SumAccess<'this, 'scope> {
338+
common: DeserializerCommon<'this, 'scope>,
335339
/// The tag of the sum value.
336-
tag: Local<'s, v8::String>,
340+
tag: Local<'scope, v8::String>,
337341
/// The value of the sum value.
338-
value: Local<'s, Value>,
342+
value: Local<'scope, Value>,
339343
}
340344

341-
impl<'de, 'a, 's: 'de> de::SumAccess<'de> for SumAccess<'a, 's> {
342-
type Error = Error<'s>;
343-
type Variant = Deserializer<'a, 's>;
345+
impl<'de, 'this, 'scope: 'de> de::SumAccess<'de> for SumAccess<'this, 'scope> {
346+
type Error = Error<'scope>;
347+
type Variant = Deserializer<'this, 'scope>;
344348

345349
fn variant<V: de::VariantVisitor<'de>>(self, visitor: V) -> Result<(V::Output, Self::Variant), Self::Error> {
346350
// Read the `tag` property in JS.
@@ -362,8 +366,8 @@ impl<'de, 'a, 's: 'de> de::SumAccess<'de> for SumAccess<'a, 's> {
362366
}
363367
}
364368

365-
impl<'de, 'a, 's: 'de> de::VariantAccess<'de> for Deserializer<'a, 's> {
366-
type Error = Error<'s>;
369+
impl<'de, 'this, 'scope: 'de> de::VariantAccess<'de> for Deserializer<'this, 'scope> {
370+
type Error = Error<'scope>;
367371

368372
fn deserialize_seed<T: de::DeserializeSeed<'de>>(self, seed: T) -> Result<T::Output, Self::Error> {
369373
seed.deserialize(self)
@@ -372,18 +376,18 @@ impl<'de, 'a, 's: 'de> de::VariantAccess<'de> for Deserializer<'a, 's> {
372376

373377
/// Used by an `ArrayVisitor` to deserialize every element of a JS array
374378
/// to a SATS array.
375-
struct ArrayAccess<'a, 's, T> {
376-
common: DeserializerCommon<'a, 's>,
377-
arr: Local<'s, Array>,
379+
struct ArrayAccess<'this, 'scope, T> {
380+
common: DeserializerCommon<'this, 'scope>,
381+
arr: Local<'scope, Array>,
378382
seeds: RepeatN<T>,
379383
index: u32,
380384
}
381385

382-
impl<'de, 'a, 's, T> ArrayAccess<'a, 's, T>
386+
impl<'de, 'this, 'scope, T> ArrayAccess<'this, 'scope, T>
383387
where
384388
T: DeserializeSeed<'de> + Clone,
385389
{
386-
fn new(arr: Local<'s, Array>, common: DeserializerCommon<'a, 's>, seed: T) -> Self {
390+
fn new(arr: Local<'scope, Array>, common: DeserializerCommon<'this, 'scope>, seed: T) -> Self {
387391
Self {
388392
arr,
389393
common,
@@ -393,9 +397,9 @@ where
393397
}
394398
}
395399

396-
impl<'de, 's: 'de, T: DeserializeSeed<'de> + Clone> de::ArrayAccess<'de> for ArrayAccess<'_, 's, T> {
400+
impl<'de, 'scope: 'de, T: DeserializeSeed<'de> + Clone> de::ArrayAccess<'de> for ArrayAccess<'_, 'scope, T> {
397401
type Element = T::Output;
398-
type Error = Error<'s>;
402+
type Error = Error<'scope>;
399403

400404
fn next_element(&mut self) -> Result<Option<Self::Element>, Self::Error> {
401405
self.seeds

crates/core/src/host/v8/error.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22
33
use v8::{Exception, HandleScope, Local, Value};
44

5-
/// The result of trying to convert a [`Value`] in scope `'s` to some type `T`.
6-
pub(super) type ValueResult<'s, T> = Result<T, Local<'s, Value>>;
5+
/// The result of trying to convert a [`Value`] in scope `'scope` to some type `T`.
6+
pub(super) type ValueResult<'scope, T> = Result<T, Local<'scope, Value>>;
77

88
/// Types that can convert into a JS string type.
99
pub(super) trait IntoJsString {
1010
/// Converts `self` into a JS string.
11-
fn into_string<'s>(self, scope: &mut HandleScope<'s>) -> Local<'s, v8::String>;
11+
fn into_string<'scope>(self, scope: &mut HandleScope<'scope>) -> Local<'scope, v8::String>;
1212
}
1313

1414
impl IntoJsString for String {
15-
fn into_string<'s>(self, scope: &mut HandleScope<'s>) -> Local<'s, v8::String> {
15+
fn into_string<'scope>(self, scope: &mut HandleScope<'scope>) -> Local<'scope, v8::String> {
1616
v8::String::new(scope, &self).unwrap()
1717
}
1818
}
1919

2020
/// Error types that can convert into JS exception values.
2121
pub(super) trait IntoException {
2222
/// Converts `self` into a JS exception value.
23-
fn into_exception<'s>(self, scope: &mut HandleScope<'s>) -> Local<'s, Value>;
23+
fn into_exception<'scope>(self, scope: &mut HandleScope<'scope>) -> Local<'scope, Value>;
2424
}
2525

2626
/// A type converting into a JS `TypeError` exception.
2727
#[derive(Copy, Clone)]
2828
pub struct TypeError<M>(pub M);
2929

3030
impl<M: IntoJsString> IntoException for TypeError<M> {
31-
fn into_exception<'s>(self, scope: &mut HandleScope<'s>) -> Local<'s, Value> {
31+
fn into_exception<'scope>(self, scope: &mut HandleScope<'scope>) -> Local<'scope, Value> {
3232
let msg = self.0.into_string(scope);
3333
Exception::type_error(scope, msg)
3434
}

crates/core/src/host/v8/from_value.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,31 @@ use v8::{BigInt, Boolean, HandleScope, Int32, Local, Number, Uint32, Value};
88
/// Types that a v8 [`Value`] can be converted into.
99
pub(super) trait FromValue: Sized {
1010
/// Converts `val` in `scope` to `Self` if possible.
11-
fn from_value<'s>(val: Local<'_, Value>, scope: &mut HandleScope<'s>) -> ValueResult<'s, Self>;
11+
fn from_value<'scope>(val: Local<'_, Value>, scope: &mut HandleScope<'scope>) -> ValueResult<'scope, Self>;
1212
}
1313

1414
/// Provides a [`FromValue`] implementation.
1515
macro_rules! impl_from_value {
1616
($ty:ty, ($val:ident, $scope:ident) => $logic:expr) => {
1717
impl FromValue for $ty {
18-
fn from_value<'s>($val: Local<'_, Value>, $scope: &mut HandleScope<'s>) -> ValueResult<'s, Self> {
18+
fn from_value<'scope>(
19+
$val: Local<'_, Value>,
20+
$scope: &mut HandleScope<'scope>,
21+
) -> ValueResult<'scope, Self> {
1922
$logic
2023
}
2124
}
2225
};
2326
}
2427

2528
/// Tries to cast `Value` into `T` or raises a JS exception as a returned `Err` value.
26-
pub(super) fn try_cast<'a, 'b, T>(
27-
scope: &mut HandleScope<'a>,
28-
val: Local<'b, Value>,
29+
pub(super) fn try_cast<'scope_a, 'scope_b, T>(
30+
scope: &mut HandleScope<'scope_a>,
31+
val: Local<'scope_b, Value>,
2932
on_err: impl FnOnce(&str) -> String,
30-
) -> ValueResult<'a, Local<'b, T>>
33+
) -> ValueResult<'scope_a, Local<'scope_b, T>>
3134
where
32-
Local<'b, T>: TryFrom<Local<'b, Value>>,
35+
Local<'scope_b, T>: TryFrom<Local<'scope_b, Value>>,
3336
{
3437
val.try_cast::<T>()
3538
.map_err(|_| TypeError(on_err(val.type_repr())).into_exception(scope))
@@ -45,13 +48,13 @@ pub(super) use cast;
4548

4649
/// Returns a JS exception value indicating that a value overflowed
4750
/// when converting to the type `rust_ty`.
48-
fn value_overflowed<'s>(rust_ty: &str, scope: &mut HandleScope<'s>) -> Local<'s, Value> {
51+
fn value_overflowed<'scope>(rust_ty: &str, scope: &mut HandleScope<'scope>) -> Local<'scope, Value> {
4952
TypeError(format!("Value overflowed `{rust_ty}`")).into_exception(scope)
5053
}
5154

5255
/// Returns a JS exception value indicating that a value underflowed
5356
/// when converting to the type `rust_ty`.
54-
fn value_underflowed<'s>(rust_ty: &str, scope: &mut HandleScope<'s>) -> Local<'s, Value> {
57+
fn value_underflowed<'scope>(rust_ty: &str, scope: &mut HandleScope<'scope>) -> Local<'scope, Value> {
5558
TypeError(format!("Value underflowed `{rust_ty}`")).into_exception(scope)
5659
}
5760

@@ -113,11 +116,11 @@ int64_from_value!(i64, i64_value);
113116
/// - `rust_ty` is the target type as a string, for errors.
114117
/// - `scope` for any JS exceptions that need to be raised.
115118
/// - `bigint` is the integer to convert.
116-
fn bigint_to_bytes<'s, const N: usize, const W: usize, const UNSIGNED: bool>(
119+
fn bigint_to_bytes<'scope, const N: usize, const W: usize, const UNSIGNED: bool>(
117120
rust_ty: &str,
118-
scope: &mut HandleScope<'s>,
121+
scope: &mut HandleScope<'scope>,
119122
bigint: &BigInt,
120-
) -> ValueResult<'s, (bool, [u8; N])>
123+
) -> ValueResult<'scope, (bool, [u8; N])>
121124
where
122125
[[u8; 8]; W]: NoUninit,
123126
[u8; N]: AnyBitPattern,

0 commit comments

Comments
 (0)