Skip to main content

ad_astra/exports/
unit.rs

1////////////////////////////////////////////////////////////////////////////////
2// This file is part of "Ad Astra", an embeddable scripting programming       //
3// language platform.                                                         //
4//                                                                            //
5// This work is proprietary software with source-available code.              //
6//                                                                            //
7// To copy, use, distribute, or contribute to this work, you must agree to    //
8// the terms of the General License Agreement:                                //
9//                                                                            //
10// https://github.com/Eliah-Lakhin/ad-astra/blob/master/EULA.md               //
11//                                                                            //
12// The agreement grants a Basic Commercial License, allowing you to use       //
13// this work in non-commercial and limited commercial products with a total   //
14// gross revenue cap. To remove this commercial limit for one of your         //
15// products, you must acquire a Full Commercial License.                      //
16//                                                                            //
17// If you contribute to the source code, documentation, or related materials, //
18// you must grant me an exclusive license to these contributions.             //
19// Contributions are governed by the "Contributions" section of the General   //
20// License Agreement.                                                         //
21//                                                                            //
22// Copying the work in parts is strictly forbidden, except as permitted       //
23// under the General License Agreement.                                       //
24//                                                                            //
25// If you do not or cannot agree to the terms of this Agreement,              //
26// do not use this work.                                                      //
27//                                                                            //
28// This work is provided "as is", without any warranties, express or implied, //
29// except where such disclaimers are legally invalid.                         //
30//                                                                            //
31// Copyright (c) 2024 Ilya Lakhin (Илья Александрович Лахин).                 //
32// All rights reserved.                                                       //
33////////////////////////////////////////////////////////////////////////////////
34
35use crate::{
36    export,
37    runtime::{
38        ops::ScriptNone,
39        Downcast,
40        Origin,
41        Provider,
42        RuntimeResult,
43        ScriptType,
44        TypeHint,
45        Upcast,
46    },
47};
48
49/// A type that represents void data: `[]`.
50#[export(include)]
51#[export(name "nil")]
52type UnitType = ();
53
54impl<'a> Downcast<'a> for UnitType {
55    #[inline(always)]
56    fn downcast(origin: Origin, provider: Provider<'a>) -> RuntimeResult<Self> {
57        provider.to_owned().take::<UnitType>(origin)
58    }
59
60    #[inline(always)]
61    fn hint() -> TypeHint {
62        TypeHint::Type(UnitType::type_meta())
63    }
64}
65
66impl<'a> Downcast<'a> for &'a UnitType {
67    #[inline(always)]
68    fn downcast(origin: Origin, provider: Provider<'a>) -> RuntimeResult<Self> {
69        provider
70            .to_borrowed(&origin)?
71            .borrow_ref::<UnitType>(origin)
72    }
73
74    #[inline(always)]
75    fn hint() -> TypeHint {
76        TypeHint::Type(UnitType::type_meta())
77    }
78}
79
80impl<'a> Downcast<'a> for &'a mut UnitType {
81    #[inline(always)]
82    fn downcast(origin: Origin, provider: Provider<'a>) -> RuntimeResult<Self> {
83        provider
84            .to_borrowed(&origin)?
85            .borrow_mut::<UnitType>(origin)
86    }
87
88    #[inline(always)]
89    fn hint() -> TypeHint {
90        TypeHint::Type(UnitType::type_meta())
91    }
92}
93
94impl<'a> Upcast<'a> for UnitType {
95    type Output = Self;
96
97    #[inline(always)]
98    fn upcast(_origin: Origin, _this: Self) -> RuntimeResult<Self::Output> {
99        Ok(())
100    }
101
102    #[inline(always)]
103    fn hint() -> TypeHint {
104        TypeHint::Type(UnitType::type_meta())
105    }
106}
107
108impl<'a> Upcast<'a> for &'a UnitType {
109    type Output = UnitType;
110
111    #[inline(always)]
112    fn upcast(_origin: Origin, _this: Self) -> RuntimeResult<Self::Output> {
113        Ok(())
114    }
115
116    #[inline(always)]
117    fn hint() -> TypeHint {
118        TypeHint::Type(UnitType::type_meta())
119    }
120}
121
122impl<'a> Upcast<'a> for &'a mut UnitType {
123    type Output = UnitType;
124
125    #[inline(always)]
126    fn upcast(_origin: Origin, _this: Self) -> RuntimeResult<Self::Output> {
127        Ok(())
128    }
129
130    #[inline(always)]
131    fn hint() -> TypeHint {
132        TypeHint::Type(UnitType::type_meta())
133    }
134}
135
136#[export(include)]
137impl ScriptNone for UnitType {}