Skip to content

Commit 9be4bc6

Browse files
author
medigor
committed
актуализировал код
1 parent d847644 commit 9be4bc6

File tree

4 files changed

+51
-67
lines changed

4 files changed

+51
-67
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ panic = "abort" # Abort on panic
1313
strip = true # Automatically strip symbols from the binary.
1414

1515
[dependencies]
16-
addin1c = "0.1"
16+
addin1c = "0.7"

src/addin1.rs

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use addin1c::{name, ParamValue, RawAddin, Tm, Variant};
1+
use addin1c::{name, CStr1C, ParamValue, RawAddin, Tm, Variant};
22

3-
const PROPS: &[&[u16]] = &[
3+
const PROPS: &[&CStr1C] = &[
44
name!("Test"),
55
name!("PropI32"),
66
name!("PropF64"),
@@ -10,7 +10,7 @@ const PROPS: &[&[u16]] = &[
1010
name!("PropBlob"),
1111
];
1212

13-
const METHODS: &[&[u16]] = &[name!("Method1"), name!("Method2")];
13+
const METHODS: &[&CStr1C] = &[name!("Method1"), name!("Method2")];
1414

1515
pub struct Addin1 {
1616
test: i32,
@@ -41,19 +41,19 @@ impl Drop for Addin1 {
4141
}
4242

4343
impl RawAddin for Addin1 {
44-
fn register_extension_as(&mut self) -> &'static [u16] {
44+
fn register_extension_as(&mut self) -> &'static CStr1C {
4545
name!("Class1")
4646
}
4747

4848
fn get_n_props(&mut self) -> usize {
4949
PROPS.len()
5050
}
5151

52-
fn find_prop(&mut self, name: &[u16]) -> Option<usize> {
52+
fn find_prop(&mut self, name: &CStr1C) -> Option<usize> {
5353
PROPS.iter().position(|&x| x == name)
5454
}
5555

56-
fn get_prop_name(&mut self, num: usize, _alias: usize) -> Option<&'static [u16]> {
56+
fn get_prop_name(&mut self, num: usize, _alias: usize) -> Option<&'static CStr1C> {
5757
PROPS.get(num).copied()
5858
}
5959

@@ -66,61 +66,61 @@ impl RawAddin for Addin1 {
6666
4 => val.set_date(self.prop_date),
6767
5 => {
6868
let s: Vec<u16> = self.prop_str.encode_utf16().collect();
69-
return val.set_str(s.as_slice());
69+
return val.set_str1c(s.as_slice()).is_ok();
7070
}
7171
6 => {
72-
return val.set_blob(self.prop_blob.as_slice());
72+
return val.set_blob(self.prop_blob.as_slice()).is_ok();
7373
}
7474
_ => return false,
7575
};
7676
true
7777
}
7878

79-
fn set_prop_val(&mut self, num: usize, val: &ParamValue) -> bool {
79+
fn set_prop_val(&mut self, num: usize, val: &Variant) -> bool {
8080
match num {
81-
0 => match val {
81+
0 => match val.get() {
8282
ParamValue::I32(x) => {
83-
self.test = *x;
83+
self.test = x;
8484
true
8585
}
8686
_ => false,
8787
},
88-
1 => match val {
88+
1 => match val.get() {
8989
ParamValue::I32(x) => {
90-
self.prop_i32 = *x;
90+
self.prop_i32 = x;
9191
true
9292
}
9393
_ => false,
9494
},
95-
2 => match val {
95+
2 => match val.get() {
9696
ParamValue::F64(x) => {
97-
self.prop_f64 = *x;
97+
self.prop_f64 = x;
9898
true
9999
}
100100
_ => false,
101101
},
102-
3 => match val {
102+
3 => match val.get() {
103103
ParamValue::Bool(x) => {
104-
self.prop_bool = *x;
104+
self.prop_bool = x;
105105
true
106106
}
107107
_ => false,
108108
},
109-
4 => match val {
109+
4 => match val.get() {
110110
ParamValue::Date(x) => {
111-
self.prop_date = *x;
111+
self.prop_date = x;
112112
true
113113
}
114114
_ => false,
115115
},
116-
5 => match val {
116+
5 => match val.get() {
117117
ParamValue::Str(x) => {
118118
self.prop_str = String::from_utf16(x).unwrap();
119119
true
120120
}
121121
_ => false,
122122
},
123-
6 => match val {
123+
6 => match val.get() {
124124
ParamValue::Blob(x) => {
125125
self.prop_blob.clear();
126126
self.prop_blob.extend_from_slice(x);
@@ -137,27 +137,18 @@ impl RawAddin for Addin1 {
137137
}
138138

139139
fn is_prop_writable(&mut self, num: usize) -> bool {
140-
match num {
141-
0 => true,
142-
1 => true,
143-
2 => true,
144-
3 => true,
145-
4 => true,
146-
5 => true,
147-
6 => true,
148-
_ => false,
149-
}
140+
matches!(num, 0..=6)
150141
}
151142

152143
fn get_n_methods(&mut self) -> usize {
153144
METHODS.len()
154145
}
155146

156-
fn find_method(&mut self, name: &[u16]) -> Option<usize> {
147+
fn find_method(&mut self, name: &CStr1C) -> Option<usize> {
157148
METHODS.iter().position(|&x| x == name)
158149
}
159150

160-
fn get_method_name(&mut self, num: usize, _alias: usize) -> Option<&'static [u16]> {
151+
fn get_method_name(&mut self, num: usize, _alias: usize) -> Option<&'static CStr1C> {
161152
METHODS.get(num).copied()
162153
}
163154

@@ -179,11 +170,7 @@ impl RawAddin for Addin1 {
179170
}
180171

181172
fn has_ret_val(&mut self, num: usize) -> bool {
182-
match num {
183-
0 => true,
184-
1 => true,
185-
_ => false,
186-
}
173+
matches!(num, 0 | 1)
187174
}
188175

189176
fn call_as_proc(&mut self, _num: usize, _params: &mut [Variant]) -> bool {
@@ -205,15 +192,15 @@ impl RawAddin for Addin1 {
205192
_ => return false,
206193
}
207194
}
208-
ret_value.set_str(buf.as_slice())
195+
ret_value.set_str1c(buf.as_slice()).is_ok()
209196
}
210197
1 => {
211198
for (i, param) in params.iter_mut().enumerate() {
212199
match param.get() {
213200
ParamValue::Empty => {
214201
if i == 0 {
215202
let s = "Return value".encode_utf16().collect::<Vec<u16>>();
216-
if !param.set_str(&s) {
203+
if param.set_str1c(s.as_slice()).is_err() {
217204
return false;
218205
}
219206
} else {

src/addin2.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use addin1c::{name, MethodInfo, Methods, ParamValue, PropInfo, SimpleAddin, Variant};
1+
use addin1c::{name, AddinResult, CStr1C, MethodInfo, Methods, PropInfo, SimpleAddin, Variant};
22

33
pub struct Addin2 {
44
prop1: i32,
@@ -9,48 +9,39 @@ impl Addin2 {
99
Addin2 { prop1: 0 }
1010
}
1111

12-
fn method1(&mut self, param: &mut Variant, ret_value: &mut Variant) -> bool {
13-
let ParamValue::I32(value) = param.get() else {
14-
return false;
15-
};
12+
fn method1(&mut self, param: &mut Variant, ret_value: &mut Variant) -> AddinResult {
13+
let value = param.get_i32()?;
1614
self.prop1 = value;
1715
ret_value.set_i32(value * 2);
18-
true
16+
Ok(())
1917
}
2018

2119
fn method2(
2220
&mut self,
2321
param1: &mut Variant,
2422
param2: &mut Variant,
2523
ret_value: &mut Variant,
26-
) -> bool {
27-
let ParamValue::I32(value1) = param1.get() else {
28-
return false;
29-
};
30-
let ParamValue::I32(value2) = param2.get() else {
31-
return false;
32-
};
24+
) -> AddinResult {
25+
let value1 = param1.get_i32()?;
26+
let value2 = param2.get_i32()?;
3327
self.prop1 = value1 + value2;
3428
ret_value.set_i32(self.prop1);
35-
true
29+
Ok(())
3630
}
3731

38-
fn set_prop1(&mut self, value: &ParamValue) -> bool {
39-
let ParamValue::I32(value) = value else {
40-
return false;
41-
};
42-
self.prop1 = *value;
43-
true
32+
fn set_prop1(&mut self, value: &Variant) -> AddinResult {
33+
self.prop1 = value.get_i32()?;
34+
Ok(())
4435
}
4536

46-
fn get_prop1(&mut self, value: &mut Variant) -> bool {
37+
fn get_prop1(&mut self, value: &mut Variant) -> AddinResult {
4738
value.set_i32(self.prop1);
48-
true
39+
Ok(())
4940
}
5041
}
5142

5243
impl SimpleAddin for Addin2 {
53-
fn name() -> &'static [u16] {
44+
fn name() -> &'static CStr1C {
5445
name!("Class2")
5546
}
5647

src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ use std::{
77
};
88

99
use addin1::Addin1;
10-
use addin2::Addin2;
1110
use addin1c::{create_component, destroy_component, name, AttachType};
11+
use addin2::Addin2;
1212

13-
pub static mut PLATFORM_CAPABILITIES: AtomicI32 = AtomicI32::new(-1);
13+
pub static PLATFORM_CAPABILITIES: AtomicI32 = AtomicI32::new(-1);
1414

15+
/// # Safety
16+
///
17+
/// Component must be non-null.
1518
#[allow(non_snake_case)]
1619
#[no_mangle]
1720
pub unsafe extern "C" fn GetClassObject(name: *const u16, component: *mut *mut c_void) -> c_long {
@@ -28,6 +31,9 @@ pub unsafe extern "C" fn GetClassObject(name: *const u16, component: *mut *mut c
2831
}
2932
}
3033

34+
/// # Safety
35+
///
36+
/// Component must be returned from `GetClassObject`, the function must be called once for each component.
3137
#[allow(non_snake_case)]
3238
#[no_mangle]
3339
pub unsafe extern "C" fn DestroyObject(component: *mut *mut c_void) -> c_long {
@@ -43,7 +49,7 @@ pub extern "C" fn GetClassNames() -> *const u16 {
4349

4450
#[allow(non_snake_case)]
4551
#[no_mangle]
46-
pub unsafe extern "C" fn SetPlatformCapabilities(capabilities: c_int) -> c_int {
52+
pub extern "C" fn SetPlatformCapabilities(capabilities: c_int) -> c_int {
4753
PLATFORM_CAPABILITIES.store(capabilities, Ordering::Relaxed);
4854
3
4955
}

0 commit comments

Comments
 (0)