oxc_allocator/
convert.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#![allow(clippy::inline_always)]

use crate::{Allocator, Box};

/// This trait works similarly to the standard library [`From`] trait, It comes with a similar
/// implementation containing blanket implementation for [`IntoIn`], reflective implementation and a
/// bunch of primitive conversions from Rust types to their arena equivalent.
pub trait FromIn<'a, T>: Sized {
    /// Converts to this type from the input type within the given `allocator`.
    fn from_in(value: T, allocator: &'a Allocator) -> Self;
}

/// This trait works similarly to the standard library [`Into`] trait.
/// It is similar to [`FromIn`] is reflective, A [`FromIn`] implementation also implicitly implements
/// [`IntoIn`] for the opposite type.
pub trait IntoIn<'a, T>: Sized {
    /// Converts this type into the (usually inferred) input type within the given `allocator`.
    fn into_in(self, allocator: &'a Allocator) -> T;
}

/// `FromIn` is reflective
impl<'a, T> FromIn<'a, T> for T {
    #[inline(always)]
    fn from_in(t: T, _: &'a Allocator) -> T {
        t
    }
}

/// `FromIn` implicitly implements `IntoIn`.
impl<'a, T, U> IntoIn<'a, U> for T
where
    U: FromIn<'a, T>,
{
    #[inline]
    fn into_in(self, allocator: &'a Allocator) -> U {
        U::from_in(self, allocator)
    }
}

// ---------------- Primitive allocations ----------------

impl<'a> FromIn<'a, String> for crate::String<'a> {
    #[inline(always)]
    fn from_in(value: String, allocator: &'a Allocator) -> Self {
        crate::String::from_str_in(value.as_str(), allocator)
    }
}

impl<'a> FromIn<'a, String> for &'a str {
    #[inline(always)]
    fn from_in(value: String, allocator: &'a Allocator) -> Self {
        crate::String::from_str_in(value.as_str(), allocator).into_bump_str()
    }
}

impl<'a, T> FromIn<'a, T> for Box<'a, T> {
    #[inline(always)]
    fn from_in(value: T, allocator: &'a Allocator) -> Self {
        Box::new_in(value, allocator)
    }
}

impl<'a, T> FromIn<'a, Option<T>> for Option<Box<'a, T>> {
    #[inline(always)]
    fn from_in(value: Option<T>, allocator: &'a Allocator) -> Self {
        value.map(|it| Box::new_in(it, allocator))
    }
}