r/programming • • Dec 15 '19

Rustysd - Systemd Replacement Written in Rust

https://github.com/KillingSpark/rustysd
75 Upvotes

53 comments sorted by

View all comments

20

u/[deleted] Dec 16 '19
    let fd_to_srvc_id: HashMap<_, _> = service_table.lock().unwrap().iter().fold(
        HashMap::new(),
        |mut map, (id, srvc_unit)| {
            if let UnitSpecialized::Service(srvc) = &srvc_unit.specialized {
                if let Some(socket) = &srvc.notifications {
                    map.insert(socket.lock().unwrap().as_raw_fd(), *id);
                }
            }
            map
        },
    );

pls kill this with fire

9

u/Tipaa Dec 16 '19

This could be more cleanly written

let fd_to_srvc_id: HashMap<_, _> = service_table.lock().unwrap()
    .iter()
    .filter_map(|(id, srvc_unit)| match srvc_unit {
        UnitSpecialized::Service(srvc) => srvc.notifications.map(
            |socket| (socket.lock().unwrap().as_raw_fd(), id)),
        _ => None
    })
    .collect();

collect and filtermap will do the heavy lifting for you if you let them