Skip to content

Commit eca4cf6

Browse files
authored
Merge pull request #841 from zeenix/interface-proxy-fixes
A few fixes & enhancements to proxy generation of interface macro
2 parents 852c187 + 9d80a63 commit eca4cf6

File tree

5 files changed

+66
-67
lines changed

5 files changed

+66
-67
lines changed

zbus/src/fdo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ assert_impl_all!(DBusProxy<'_>: Send, Sync, Unpin);
827827
#[zbus(prefix = "org.freedesktop.DBus.Error", impl_display = true)]
828828
#[allow(clippy::upper_case_acronyms)]
829829
pub enum Error {
830-
/// Unknown or fall-through ZBus error.
830+
/// Unknown or fall-through zbus error.
831831
#[zbus(error)]
832832
ZBus(zbus::Error),
833833

zbus_macros/src/iface.rs

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ struct MethodInfo {
169169
output: ReturnType,
170170
/// The cfg attributes of the method.
171171
cfg_attrs: Vec<Attribute>,
172+
/// The doc attributes of the method.
173+
doc_attrs: Vec<Attribute>,
172174
}
173175

174176
impl MethodInfo {
@@ -177,6 +179,7 @@ impl MethodInfo {
177179
method: &ImplItemFn,
178180
attrs: &MethodAttrs,
179181
cfg_attrs: &[&Attribute],
182+
doc_attrs: &[&Attribute],
180183
) -> syn::Result<MethodInfo> {
181184
let is_async = method.sig.asyncness.is_some();
182185
let Signature {
@@ -319,6 +322,7 @@ impl MethodInfo {
319322
proxy_attrs,
320323
output: output.clone(),
321324
cfg_attrs: cfg_attrs.iter().cloned().cloned().collect(),
325+
doc_attrs: doc_attrs.iter().cloned().cloned().collect(),
322326
})
323327
}
324328
}
@@ -354,12 +358,7 @@ pub fn expand<T: AttrParse + Into<ImplAttrs>, M: AttrParse + Into<MethodAttrs>>(
354358

355359
let (iface_name, with_spawn, mut proxy) = {
356360
let (name, interface, spawn, proxy) = match T::parse_nested_metas(args)?.into() {
357-
ImplAttrs::New(new) => (
358-
new.name,
359-
new.interface,
360-
new.spawn,
361-
new.proxy.map(|p| Proxy::new(ty, p)),
362-
),
361+
ImplAttrs::New(new) => (new.name, new.interface, new.spawn, new.proxy),
363362
// New proxy attributes are not supported for old `dbus_interface`.
364363
ImplAttrs::Old(old) => (old.name, old.interface, old.spawn, None),
365364
};
@@ -373,6 +372,7 @@ pub fn expand<T: AttrParse + Into<ImplAttrs>, M: AttrParse + Into<MethodAttrs>>(
373372
"`name` and `interface` attributes should not be specified at the same time",
374373
)),
375374
};
375+
let proxy = proxy.map(|p| Proxy::new(ty, &name, p, &zbus));
376376

377377
(name, !spawn.unwrap_or(false), proxy)
378378
};
@@ -424,8 +424,13 @@ pub fn expand<T: AttrParse + Into<ImplAttrs>, M: AttrParse + Into<MethodAttrs>>(
424424
.iter()
425425
.filter(|a| a.path().is_ident("cfg"))
426426
.collect();
427+
let doc_attrs: Vec<_> = method
428+
.attrs
429+
.iter()
430+
.filter(|a| a.path().is_ident("doc"))
431+
.collect();
427432

428-
let method_info = MethodInfo::new(&zbus, method, &attrs, &cfg_attrs)?;
433+
let method_info = MethodInfo::new(&zbus, method, &attrs, &cfg_attrs, &doc_attrs)?;
429434
let attr_property = match attrs {
430435
MethodAttrs::Old(o) => o.property.map(|op| PropertyAttributes {
431436
emits_changed_signal: op.emits_changed_signal,
@@ -773,7 +778,7 @@ pub fn expand<T: AttrParse + Into<ImplAttrs>, M: AttrParse + Into<MethodAttrs>>(
773778
}
774779

775780
if let Some(proxy) = &mut proxy {
776-
proxy.add_method(info, &properties, &zbus);
781+
proxy.add_method(info, &properties);
777782
}
778783
}
779784

@@ -794,7 +799,7 @@ pub fn expand<T: AttrParse + Into<ImplAttrs>, M: AttrParse + Into<MethodAttrs>>(
794799
}
795800
};
796801

797-
let proxy = proxy.map(|proxy| proxy.gen(&iface_name, &zbus));
802+
let proxy = proxy.map(|proxy| proxy.gen());
798803

799804
Ok(quote! {
800805
#input
@@ -1115,7 +1120,7 @@ fn introspect_output_arg(
11151120
)
11161121
}
11171122

1118-
fn get_result_type(p: &TypePath) -> syn::Result<&Type> {
1123+
fn get_result_inner_type(p: &TypePath) -> syn::Result<&Type> {
11191124
if let PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }) = &p
11201125
.path
11211126
.segments
@@ -1151,7 +1156,7 @@ fn introspect_add_output_args(
11511156
.ident
11521157
== "Result";
11531158
if is_result_output {
1154-
ty = get_result_type(p)?;
1159+
ty = get_result_inner_type(p)?;
11551160
}
11561161
}
11571162

@@ -1187,7 +1192,7 @@ fn get_return_type(output: &ReturnType) -> syn::Result<&Type> {
11871192
.ident
11881193
== "Result";
11891194
if is_result_output {
1190-
return get_result_type(p);
1195+
return get_result_inner_type(p);
11911196
}
11921197
}
11931198

@@ -1307,6 +1312,10 @@ impl Parse for ImplItemSignal {
13071312
struct Proxy {
13081313
// The type name
13091314
ty: Ident,
1315+
// The interface name
1316+
iface_name: String,
1317+
// The zbus crate
1318+
zbus: TokenStream,
13101319

13111320
// Input
13121321
attrs: ProxyAttributes,
@@ -1316,34 +1325,25 @@ struct Proxy {
13161325
}
13171326

13181327
impl Proxy {
1319-
fn new(ty: &Ident, attrs: ProxyAttributes) -> Self {
1328+
fn new(ty: &Ident, iface_name: &str, attrs: ProxyAttributes, zbus: &TokenStream) -> Self {
13201329
Self {
1330+
iface_name: iface_name.to_string(),
13211331
ty: ty.clone(),
1332+
zbus: zbus.clone(),
13221333
attrs,
13231334
methods: quote!(),
13241335
}
13251336
}
13261337

1327-
fn add_method(
1328-
&mut self,
1329-
method_info: MethodInfo,
1330-
properties: &BTreeMap<String, Property<'_>>,
1331-
zbus: &TokenStream,
1332-
) {
1338+
fn add_method(&mut self, method_info: MethodInfo, properties: &BTreeMap<String, Property<'_>>) {
13331339
let inputs: Punctuated<PatType, Comma> = method_info
13341340
.typed_inputs
13351341
.iter()
1336-
.enumerate()
1337-
.filter(|(idx, input)| {
1338-
if method_info.method_type == MethodType::Signal {
1339-
// Skip the `SignalContext` argument.
1340-
return *idx != 0;
1341-
}
1342-
1342+
.filter(|input| {
13431343
let a = ArgAttributes::parse(&input.attrs).unwrap();
13441344
!a.object_server && !a.connection && !a.header && !a.signal_context
13451345
})
1346-
.map(|(_, p)| p.clone())
1346+
.cloned()
13471347
.collect();
13481348
let ret = get_return_type(&method_info.output)
13491349
.map(|r| quote!(#r))
@@ -1386,14 +1386,17 @@ impl Proxy {
13861386
}
13871387
}
13881388
let cfg_attrs = method_info.cfg_attrs;
1389+
let zbus = &self.zbus;
1390+
let doc_attrs = method_info.doc_attrs;
13891391
self.methods.extend(quote! {
13901392
#(#cfg_attrs)*
1393+
#(#doc_attrs)*
13911394
#[zbus(#proxy_method_attrs)]
13921395
fn #ident(&self, #inputs) -> #zbus::Result<#ret>;
13931396
});
13941397
}
13951398

1396-
fn gen(&self, iface_name: &str, zbus: &TokenStream) -> TokenStream {
1399+
fn gen(&self) -> TokenStream {
13971400
let attrs = &self.attrs;
13981401
let (
13991402
assume_defaults,
@@ -1432,7 +1435,11 @@ impl Proxy {
14321435
&self.ty,
14331436
&self.methods,
14341437
);
1438+
let iface_name = &self.iface_name;
1439+
let zbus = &self.zbus;
1440+
let proxy_doc = format!("Proxy for the `{iface_name}` interface.");
14351441
quote! {
1442+
#[doc = #proxy_doc]
14361443
#[#zbus::proxy(
14371444
name = #iface_name,
14381445
#assume_defaults

zvariant/src/lib.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -820,13 +820,13 @@ mod tests {
820820
// Signature: "a(yu(xbxas)s)");
821821
let ar = vec![(
822822
// top-most simple fields
823-
u8::max_value(),
824-
u32::max_value(),
823+
u8::MAX,
824+
u32::MAX,
825825
(
826826
// 2nd level simple fields
827-
i64::max_value(),
827+
i64::MAX,
828828
true,
829-
i64::max_value(),
829+
i64::MAX,
830830
// 2nd level array field
831831
&["Hello", "World"][..],
832832
),
@@ -841,12 +841,12 @@ mod tests {
841841
encoded.deserialize().unwrap().0;
842842
assert_eq!(decoded.len(), 1);
843843
let r = &decoded[0];
844-
assert_eq!(r.0, u8::max_value());
845-
assert_eq!(r.1, u32::max_value());
844+
assert_eq!(r.0, u8::MAX);
845+
assert_eq!(r.1, u32::MAX);
846846
let inner_r = &r.2;
847-
assert_eq!(inner_r.0, i64::max_value());
847+
assert_eq!(inner_r.0, i64::MAX);
848848
assert!(inner_r.1);
849-
assert_eq!(inner_r.2, i64::max_value());
849+
assert_eq!(inner_r.2, i64::MAX);
850850
let as_ = &inner_r.3;
851851
assert_eq!(as_.len(), 2);
852852
assert_eq!(as_[0], "Hello");
@@ -863,12 +863,12 @@ mod tests {
863863
gv_encoded.deserialize().unwrap().0;
864864
assert_eq!(decoded.len(), 1);
865865
let r = &decoded[0];
866-
assert_eq!(r.0, u8::max_value());
867-
assert_eq!(r.1, u32::max_value());
866+
assert_eq!(r.0, u8::MAX);
867+
assert_eq!(r.1, u32::MAX);
868868
let inner_r = &r.2;
869-
assert_eq!(inner_r.0, i64::max_value());
869+
assert_eq!(inner_r.0, i64::MAX);
870870
assert!(inner_r.1);
871-
assert_eq!(inner_r.2, i64::max_value());
871+
assert_eq!(inner_r.2, i64::MAX);
872872
let as_ = &inner_r.3;
873873
assert_eq!(as_.len(), 2);
874874
assert_eq!(as_[0], "Hello");
@@ -883,8 +883,8 @@ mod tests {
883883
assert_eq!(variant.n_children(), 1);
884884
let r: (u8, u32, (i64, bool, i64, Vec<String>), String) =
885885
variant.child_value(0).get().unwrap();
886-
assert_eq!(r.0, u8::max_value());
887-
assert_eq!(r.1, u32::max_value());
886+
assert_eq!(r.0, u8::MAX);
887+
assert_eq!(r.1, u32::MAX);
888888
}
889889
let ctxt = Context::new_dbus(LE, 0);
890890

@@ -900,13 +900,13 @@ mod tests {
900900
let r = &array[0];
901901
if let Value::Structure(r) = r {
902902
let fields = r.fields();
903-
assert_eq!(fields[0], Value::U8(u8::max_value()));
904-
assert_eq!(fields[1], Value::U32(u32::max_value()));
903+
assert_eq!(fields[0], Value::U8(u8::MAX));
904+
assert_eq!(fields[1], Value::U32(u32::MAX));
905905
if let Value::Structure(r) = &fields[2] {
906906
let fields = r.fields();
907-
assert_eq!(fields[0], Value::I64(i64::max_value()));
907+
assert_eq!(fields[0], Value::I64(i64::MAX));
908908
assert_eq!(fields[1], Value::Bool(true));
909-
assert_eq!(fields[2], Value::I64(i64::max_value()));
909+
assert_eq!(fields[2], Value::I64(i64::MAX));
910910
if let Value::Array(as_) = &fields[3] {
911911
assert_eq!(as_.len(), 2);
912912
assert_eq!(as_[0], Value::new("Hello"));
@@ -940,13 +940,13 @@ mod tests {
940940
let r = &array.get(0).unwrap().unwrap();
941941
if let Value::Structure(r) = r {
942942
let fields = r.fields();
943-
assert_eq!(fields[0], Value::U8(u8::max_value()));
944-
assert_eq!(fields[1], Value::U32(u32::max_value()));
943+
assert_eq!(fields[0], Value::U8(u8::MAX));
944+
assert_eq!(fields[1], Value::U32(u32::MAX));
945945
if let Value::Structure(r) = &fields[2] {
946946
let fields = r.fields();
947-
assert_eq!(fields[0], Value::I64(i64::max_value()));
947+
assert_eq!(fields[0], Value::I64(i64::MAX));
948948
assert_eq!(fields[1], Value::Bool(true));
949-
assert_eq!(fields[2], Value::I64(i64::max_value()));
949+
assert_eq!(fields[2], Value::I64(i64::MAX));
950950
if let Value::Array(as_) = &fields[3] {
951951
assert_eq!(as_.len(), 2);
952952
assert_eq!(as_.get(0).unwrap(), Some("Hello"));
@@ -972,8 +972,8 @@ mod tests {
972972
let child: Variant = variant.child_value(0);
973973
let r: (u8, u32, (i64, bool, i64, Vec<String>), String) =
974974
child.child_value(0).get().unwrap();
975-
assert_eq!(r.0, u8::max_value());
976-
assert_eq!(r.1, u32::max_value());
975+
assert_eq!(r.0, u8::MAX);
976+
assert_eq!(r.1, u32::MAX);
977977

978978
let mut rng = thread_rng();
979979
// Let's test GVariant ser/de of a 254 byte array with variable-width elements as to
@@ -1165,9 +1165,9 @@ mod tests {
11651165
let expect_iter = expect.iter().map(|(k, v)| (k, v)).collect::<Vec<_>>();
11661166
let actual = dict.iter().collect::<Vec<_>>();
11671167
assert_eq!(actual, expect_iter);
1168-
let actual = (&dict).iter().collect::<Vec<_>>();
1168+
let actual = dict.iter().collect::<Vec<_>>();
11691169
assert_eq!(actual, expect_iter);
1170-
let actual = (&mut dict).iter().collect::<Vec<_>>();
1170+
let actual = dict.iter().collect::<Vec<_>>();
11711171
assert_eq!(actual, expect_iter);
11721172
for (_, v) in dict.iter_mut() {
11731173
if let Value::Str(vv) = v {

zvariant/src/ser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Write for NullWriteSeek {
2929

3030
impl Seek for NullWriteSeek {
3131
fn seek(&mut self, _pos: std::io::SeekFrom) -> std::io::Result<u64> {
32-
Ok(std::u64::MAX) // should never read the return value!
32+
Ok(u64::MAX) // should never read the return value!
3333
}
3434
}
3535

zvariant/src/utils.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub(crate) fn padding_for_n_bytes(value: usize, align: usize) -> usize {
5858

5959
pub(crate) fn usize_to_u32(value: usize) -> u32 {
6060
assert!(
61-
value <= (std::u32::MAX as usize),
61+
value <= (u32::MAX as usize),
6262
"{} too large for `u32`",
6363
value,
6464
);
@@ -67,21 +67,13 @@ pub(crate) fn usize_to_u32(value: usize) -> u32 {
6767
}
6868

6969
pub(crate) fn usize_to_u8(value: usize) -> u8 {
70-
assert!(
71-
value <= (std::u8::MAX as usize),
72-
"{} too large for `u8`",
73-
value,
74-
);
70+
assert!(value <= (u8::MAX as usize), "{} too large for `u8`", value,);
7571

7672
value as u8
7773
}
7874

7975
pub(crate) fn f64_to_f32(value: f64) -> f32 {
80-
assert!(
81-
value <= (std::f32::MAX as f64),
82-
"{} too large for `f32`",
83-
value,
84-
);
76+
assert!(value <= (f32::MAX as f64), "{} too large for `f32`", value,);
8577

8678
value as f32
8779
}

0 commit comments

Comments
 (0)