Skip to main content

fre_rs/types/
primitive.rs

1use super::*;
2use std::string::String as StdString;
3
4
5crate::class! {
6    /// A reference to the AS3 object `int`.
7    /// 
8    /// Some methods are not yet implemented.
9    /// 
10    #[allow(non_camel_case_types)]
11    int: ?PartialEq
12}
13impl PartialEq for int<'_> {fn eq(&self, other: &Self) -> bool {self.value() == other.value()}}
14impl PartialEq<i32> for int<'_> {fn eq(&self, other: &i32) -> bool {self.value() == *other}}
15impl PartialEq<int<'_>> for i32 {fn eq(&self, other: &int<'_>) -> bool {*self == other.value()}}
16impl Eq for int<'_> {}
17impl<'a> int<'a> {
18    pub fn new (_: &CurrentContext<'a>, value: i32) -> Self {
19        let mut object = MaybeUninit::<FREObject>::uninit();
20        let r = unsafe {FRENewObjectFromInt32(value, object.as_mut_ptr())};
21        debug_assert!(r.is_ok());
22        let object = unsafe {object.assume_init()};
23        assert!(!object.is_null());
24        unsafe {transmute(object)}
25    }
26    pub fn value(self) -> i32 {
27        let mut value = MaybeUninit::<i32>::uninit();
28        let r = unsafe {FREGetObjectAsInt32(self.as_ptr(), value.as_mut_ptr())};
29        debug_assert!(r.is_ok());
30        unsafe {value.assume_init()}
31    }
32}
33
34
35crate::class! {
36    /// A reference to the AS3 object `uint`.
37    /// 
38    /// Some methods are not yet implemented.
39    /// 
40    #[allow(non_camel_case_types)]
41    uint: ?PartialEq
42}
43impl PartialEq for uint<'_> {fn eq(&self, other: &Self) -> bool {self.value() == other.value()}}
44impl PartialEq<u32> for uint<'_> {fn eq(&self, other: &u32) -> bool {self.value() == *other}}
45impl PartialEq<uint<'_>> for u32 {fn eq(&self, other: &uint<'_>) -> bool {*self == other.value()}}
46impl Eq for uint<'_> {}
47impl<'a> uint<'a> {
48    pub fn new (_: &CurrentContext<'a>, value: u32) -> Self {
49        let mut object = MaybeUninit::<FREObject>::uninit();
50        let r = unsafe {FRENewObjectFromUint32(value, object.as_mut_ptr())};
51        debug_assert!(r.is_ok());
52        let object = unsafe {object.assume_init()};
53        assert!(!object.is_null());
54        unsafe {transmute(object)}
55    }
56    pub fn value (self) -> u32 {
57        let mut value = MaybeUninit::<u32>::uninit();
58        let r = unsafe {FREGetObjectAsUint32(self.as_ptr(), value.as_mut_ptr())};
59        debug_assert!(r.is_ok());
60        unsafe {value.assume_init()}
61    }
62}
63
64
65crate::class! {@Typeof
66    /// A reference to the AS3 object `Number`.
67    /// 
68    /// Some methods are not yet implemented.
69    /// 
70    Number: ?PartialEq
71}
72impl PartialEq for Number<'_> {fn eq(&self, other: &Self) -> bool {self.value() == other.value()}}
73impl PartialEq<f64> for Number<'_> {fn eq(&self, other: &f64) -> bool {self.value() == *other}}
74impl PartialEq<Number<'_>> for f64 {fn eq(&self, other: &Number<'_>) -> bool {*self == other.value()}}
75impl<'a> Number<'a> {
76    pub fn new (_: &CurrentContext<'a>, value: f64) -> Self {
77        let mut object = MaybeUninit::<FREObject>::uninit();
78        let r = unsafe {FRENewObjectFromDouble(value, object.as_mut_ptr())};
79        debug_assert!(r.is_ok());
80        let object = unsafe {object.assume_init()};
81        assert!(!object.is_null());
82        unsafe {transmute(object)}
83    }
84    pub fn value (self) -> f64 {
85        let mut value = MaybeUninit::<f64>::uninit();
86        let r = unsafe {FREGetObjectAsDouble(self.as_ptr(), value.as_mut_ptr())};
87        debug_assert!(r.is_ok());
88        unsafe {value.assume_init()}
89    }
90}
91
92
93crate::class! {@Typeof
94    /// A reference to the AS3 object `Boolean`.
95    /// 
96    Boolean: ?PartialEq
97}
98impl PartialEq for Boolean<'_> {fn eq(&self, other: &Self) -> bool {self.value() == other.value()}}
99impl PartialEq<bool> for Boolean<'_> {fn eq(&self, other: &bool) -> bool {self.value() == *other}}
100impl PartialEq<Boolean<'_>> for bool {fn eq(&self, other: &Boolean<'_>) -> bool {*self == other.value()}}
101impl Eq for Boolean<'_> {}
102impl<'a> Boolean<'a> {
103    pub fn new (_: &CurrentContext<'a>, value: bool) -> Self {
104        let value = if value {1} else {0};
105        let mut object = MaybeUninit::<FREObject>::uninit();
106        let r = unsafe {FRENewObjectFromBool(value, object.as_mut_ptr())};
107        debug_assert!(r.is_ok());
108        let object = unsafe {object.assume_init()};
109        assert!(!object.is_null());
110        unsafe {transmute(object)}
111    }
112    pub fn value (self) -> bool {
113        let mut value = MaybeUninit::<u32>::uninit();
114        let r = unsafe {FREGetObjectAsBool(self.as_ptr(), value.as_mut_ptr())};
115        debug_assert!(r.is_ok());
116        let value: u32 = unsafe {value.assume_init()};
117        value != 0
118    }
119}
120
121
122//
123// TODO: Implement methods of `String`.
124//
125crate::class! {@Typeof
126    /// A reference to the AS3 object `String`.
127    /// 
128    /// Some methods are not yet implemented.
129    /// 
130    String: ?PartialEq
131}
132impl PartialEq for String<'_> {fn eq(&self, other: &Self) -> bool {self.value() == other.value()}}
133impl PartialEq<str> for String<'_> {fn eq(&self, other: &str) -> bool {self.value() == other}}
134impl PartialEq<StdString> for String<'_> {fn eq(&self, other: &StdString) -> bool {self.value() == other.as_str()}}
135impl PartialEq<Cow<'_, str>> for String<'_> {fn eq(&self, other: &Cow<'_, str>) -> bool {self.value() == other.as_ref()}}
136impl PartialEq<Box<str>> for String<'_> {fn eq(&self, other: &Box<str>) -> bool {self.value() == other.as_ref()}}
137impl PartialEq<Rc<str>> for String<'_> {fn eq(&self, other: &Rc<str>) -> bool {self.value() == other.as_ref()}}
138impl PartialEq<Arc<str>> for String<'_> {fn eq(&self, other: &Arc<str>) -> bool {self.value() == other.as_ref()}}
139impl PartialEq<UCStr> for String<'_> {fn eq(&self, other: &UCStr) -> bool {self.value() == other.as_str()}}
140impl PartialEq<String<'_>> for str {fn eq(&self, other: &String<'_>) -> bool {self == other.value()}}
141impl PartialEq<String<'_>> for StdString {fn eq(&self, other: &String<'_>) -> bool {PartialEq::eq(other, self)}}
142impl PartialEq<String<'_>> for Cow<'_, str> {fn eq(&self, other: &String<'_>) -> bool {PartialEq::eq(other, self)}}
143impl PartialEq<String<'_>> for Box<str> {fn eq(&self, other: &String<'_>) -> bool {PartialEq::eq(other, self)}}
144impl PartialEq<String<'_>> for Rc<str> {fn eq(&self, other: &String<'_>) -> bool {PartialEq::eq(other, self)}}
145impl PartialEq<String<'_>> for Arc<str> {fn eq(&self, other: &String<'_>) -> bool {PartialEq::eq(other, self)}}
146impl PartialEq<String<'_>> for UCStr {fn eq(&self, other: &String<'_>) -> bool {PartialEq::eq(other, self)}}
147impl Eq for String<'_> {}
148impl<'a> String<'a> {
149    pub fn new (_: &CurrentContext<'a>, value: &str) -> Self {
150        let value = value.as_bytes();
151        debug_assert!(value.len() <= u32::MAX as usize);
152        let mut object = MaybeUninit::<FREObject>::uninit();
153        let r = unsafe {FRENewObjectFromUTF8(value.len() as u32, value.as_ptr(), object.as_mut_ptr())};
154        debug_assert!(r.is_ok());
155        let object = unsafe {object.assume_init()};
156        assert!(!object.is_null());
157        unsafe {transmute(object)}
158    }
159    pub fn value (self) -> &'a str {
160        let mut len = MaybeUninit::<u32>::uninit();
161        let mut ptr = MaybeUninit::<FREStr>::uninit();
162        let r = unsafe {FREGetObjectAsUTF8(self.as_ptr(), len.as_mut_ptr(), ptr.as_mut_ptr())};
163        debug_assert!(r.is_ok());
164        let len = unsafe {len.assume_init()};
165        let ptr = unsafe {ptr.assume_init()};
166        assert!(!ptr.is_null());
167        let bytes = unsafe {std::slice::from_raw_parts(ptr, len as usize)};
168        unsafe {str::from_utf8_unchecked(bytes)}
169    }
170}
171impl<'a> AsRef<str> for String<'a> {fn as_ref(&self) -> &'a str {self.value()}}
172