Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ec4ecd55 authored by Ying Hsu's avatar Ying Hsu Committed by Automerger Merge Worker
Browse files

Merge "Reland: floss: Support HashMap<K,V> in D-Bus projection property-map" am: fbf15375

parents 2aa0f94d fbf15375
Loading
Loading
Loading
Loading
+79 −0
Original line number Diff line number Diff line
@@ -854,6 +854,7 @@ pub fn generate_dbus_arg(_item: TokenStream) -> TokenStream {

        use std::error::Error;
        use std::fmt;
        use std::hash::Hash;
        use std::sync::{Arc, Mutex};

        // Key for serialized Option<T> in propmap
@@ -991,6 +992,34 @@ pub fn generate_dbus_arg(_item: TokenStream) -> TokenStream {
            }
        }

        impl<
                K: 'static + Eq + Hash + RefArgToRust<RustType = K>,
                V: 'static + RefArgToRust<RustType = V>
            > RefArgToRust for std::collections::HashMap<K, V>
        {
            type RustType = std::collections::HashMap<K, V>;

            fn ref_arg_to_rust(
                arg: &(dyn dbus::arg::RefArg + 'static),
                name: String,
            ) -> Result<Self::RustType, Box<dyn Error>> {
                let mut map: std::collections::HashMap<K, V> = std::collections::HashMap::new();
                let mut iter = arg.as_iter().unwrap();
                let mut key = iter.next();
                let mut val = iter.next();
                while !key.is_none() && !val.is_none() {
                    let k = key.unwrap().box_clone();
                    let k = <K as RefArgToRust>::ref_arg_to_rust(&k, name.clone() + " key")?;
                    let v = val.unwrap().box_clone();
                    let v = <V as RefArgToRust>::ref_arg_to_rust(&v, name.clone() + " value")?;
                    map.insert(k, v);
                    key = iter.next();
                    val = iter.next();
                }
                Ok(map)
            }
        }

        /// Trait describing how to convert to and from a D-Bus type.
        ///
        /// All Rust structs that need to be serialized to and from D-Bus need
@@ -1153,6 +1182,56 @@ pub fn generate_dbus_arg(_item: TokenStream) -> TokenStream {
                Ok(props)
            }
        }

        impl<K: Eq + Hash + DBusArg, V: DBusArg> DBusArg for std::collections::HashMap<K, V>
            where
                <K as DBusArg>::DBusType: 'static
                    + Eq
                    + Hash
                    + dbus::arg::RefArg
                    + RefArgToRust<RustType = <K as DBusArg>::DBusType>,
        {
            type DBusType = std::collections::HashMap<K::DBusType, V::DBusType>;

            fn from_dbus(
                data: std::collections::HashMap<K::DBusType, V::DBusType>,
                conn: Option<std::sync::Arc<dbus::nonblock::SyncConnection>>,
                remote: Option<dbus::strings::BusName<'static>>,
                disconnect_watcher: Option<
                    std::sync::Arc<std::sync::Mutex<dbus_projection::DisconnectWatcher>>>,
            ) -> Result<std::collections::HashMap<K, V>, Box<dyn std::error::Error>> {
                let mut map = std::collections::HashMap::new();
                for (key, val) in data {
                    let k = K::from_dbus(
                        key,
                        conn.clone(),
                        remote.clone(),
                        disconnect_watcher.clone()
                    )?;
                    let v = V::from_dbus(
                        val,
                        conn.clone(),
                        remote.clone(),
                        disconnect_watcher.clone()
                    )?;
                    map.insert(k, v);
                }
                Ok(map)
            }

            fn to_dbus(
                data: std::collections::HashMap<K, V>,
            ) -> Result<std::collections::HashMap<K::DBusType, V::DBusType>, Box<dyn std::error::Error>>
            {
                let mut map = std::collections::HashMap::new();
                for (key, val) in data {
                    let k = K::to_dbus(key)?;
                    let v = V::to_dbus(val)?;
                    map.insert(k, v);
                }
                Ok(map)
            }
        }
    };

    debug_output_to_file(&gen, format!("out-generate_dbus_arg.rs"));
+25 −0
Original line number Diff line number Diff line
use core::any::Any;
use std::collections::HashMap;

use dbus_macros::{dbus_propmap, generate_dbus_arg};

@@ -23,6 +24,7 @@ struct SomeStruct {
    number: i32,
    other_struct: OtherStruct,
    bytes: Vec<u8>,
    dict: HashMap<String, Vec<i32>>,
    nested: Vec<Vec<String>>,
    recursive: Vec<SomeStruct>,
}
@@ -33,6 +35,7 @@ struct SomeStructDBus {
    number: i32,
    other_struct: OtherStruct,
    bytes: Vec<u8>,
    dict: HashMap<String, Vec<i32>>,
    nested: Vec<Vec<String>>,
    recursive: Vec<SomeStruct>,
}
@@ -112,6 +115,13 @@ mod tests {
                    }),
                ),
                (String::from("bytes"), Box::new(vec![1 as u8, 2, 3])),
                (
                    String::from("dict"),
                    Box::new(HashMap::from([
                        (String::from("key-0"), Box::new(vec![5, 6, 7, 8])),
                        (String::from("key-1"), Box::new(vec![-5, -6, -7, -8])),
                    ])),
                ),
                (
                    String::from("nested"),
                    Box::new(vec![
@@ -139,6 +149,13 @@ mod tests {
                                }),
                            ),
                            (String::from("bytes"), Box::new(Vec::<u8>::new())),
                            (
                                String::from("dict"),
                                Box::new(HashMap::from([
                                    (String::from("key-2"), Box::new(vec![5, 5, 6, 8, 8])),
                                    (String::from("key-3"), Box::new(vec![])),
                                ])),
                            ),
                            (String::from("nested"), Box::new(Vec::<Vec<u8>>::new())),
                            (String::from("recursive"), Box::new(Vec::<FakeDictionary>::new())),
                        ],
@@ -158,6 +175,10 @@ mod tests {
            number: 100,
            other_struct: OtherStruct { address: String::from("aa:bb:cc:dd:ee:ff") },
            bytes: vec![1, 2, 3],
            dict: HashMap::from([
                (String::from("key-0"), vec![5, 6, 7, 8]),
                (String::from("key-1"), vec![-5, -6, -7, -8]),
            ]),
            nested: vec![
                vec![String::from("string a"), String::from("string b"), String::from("string c")],
                vec![String::from("string 1"), String::from("string 2")],
@@ -167,6 +188,10 @@ mod tests {
                number: 200,
                other_struct: OtherStruct { address: String::from("xx") },
                bytes: vec![],
                dict: HashMap::from([
                    (String::from("key-2"), vec![5, 5, 6, 8, 8]),
                    (String::from("key-3"), vec![]),
                ]),
                nested: vec![],
                recursive: vec![],
            }],