rquickjs_core/value/convert/
atom.rs

1use crate::{
2    atom::PredefinedAtom, qjs, Atom, Ctx, FromAtom, IntoAtom, Result, StdString, String, Value,
3};
4
5impl<'js> FromAtom<'js> for Atom<'js> {
6    fn from_atom(atom: Atom<'js>) -> Result<Self> {
7        Ok(atom)
8    }
9}
10
11impl<'js> FromAtom<'js> for Value<'js> {
12    fn from_atom(atom: Atom<'js>) -> Result<Self> {
13        atom.to_value()
14    }
15}
16
17impl<'js> FromAtom<'js> for String<'js> {
18    fn from_atom(atom: Atom<'js>) -> Result<Self> {
19        atom.to_js_string()
20    }
21}
22
23impl<'js> FromAtom<'js> for StdString {
24    fn from_atom(atom: Atom<'js>) -> Result<Self> {
25        atom.to_string()
26    }
27}
28
29impl<'js> IntoAtom<'js> for PredefinedAtom {
30    fn into_atom(self, ctx: &Ctx<'js>) -> Result<Atom<'js>> {
31        Ok(unsafe { Atom::from_atom_val_dup(ctx.clone(), self as qjs::JSAtom) })
32    }
33}
34
35impl<'js> IntoAtom<'js> for Atom<'js> {
36    fn into_atom(self, _: &Ctx<'js>) -> Result<Atom<'js>> {
37        Ok(self)
38    }
39}
40
41impl<'js> IntoAtom<'js> for Value<'js> {
42    fn into_atom(self, ctx: &Ctx<'js>) -> Result<Atom<'js>> {
43        Atom::from_value(ctx.clone(), &self)
44    }
45}
46
47impl<'js> IntoAtom<'js> for &str {
48    fn into_atom(self, ctx: &Ctx<'js>) -> Result<Atom<'js>> {
49        Atom::from_str(ctx.clone(), self)
50    }
51}
52
53impl<'js> IntoAtom<'js> for StdString {
54    fn into_atom(self, ctx: &Ctx<'js>) -> Result<Atom<'js>> {
55        Atom::from_str(ctx.clone(), &self)
56    }
57}
58
59impl<'js> IntoAtom<'js> for &StdString {
60    fn into_atom(self, ctx: &Ctx<'js>) -> Result<Atom<'js>> {
61        Atom::from_str(ctx.clone(), self)
62    }
63}
64
65macro_rules! into_atom_impls {
66	  ($($from:ident: $($type:ident)*,)*) => {
67		    $(
68            $(
69                impl<'js> IntoAtom<'js> for $type {
70                    fn into_atom(self, ctx: &Ctx<'js>) -> Result<Atom<'js>> {
71                        Atom::$from(ctx.clone(), self as _)
72                    }
73                }
74            )*
75        )*
76	  };
77}
78
79into_atom_impls! {
80    from_bool: bool,
81    from_u32: u8 u16 u32,
82    from_i32: i8 i16 i32,
83    from_f64: f32 f64,
84}