quartiq / miniconf

Rust serialize/deserialize/access reflection for trees (no_std, no alloc)
MIT License
23 stars 2 forks source link

Set arrays directly through miniconf #79

Closed svrotter closed 1 year ago

svrotter commented 2 years ago

The current software state only allows setting arrays as a whole through miniconf when they are a field of a struct. This works fine, like for the coefficients of the IIR filters, but it would be nice if this extra step could be omitted if just a single array in settings needs to be changed like the AFE gains

ryan-summers commented 2 years ago

I don't think this is anything we can change. If we allowed setting an entire array directly, we would have to instead impl MiniconfAtomic for an array type. However, our default implementation currently implements Miniconf directly for arrays to access sub-members, as this is the most commonly-used path.

Perhaps we could provide a wrapper type for arrays that would implement atomic updates on them, e.g.:

let settable_array = MiniconfAtomicArray([0u8; 10]);
jordens commented 2 years ago

We could introduce attributes for struct members that control this in the same way serde does it.

ryan-summers commented 2 years ago

I'm not sure that would solve things here, since the proc-macro #[derive(Miniconf)] just defers to members implementation of the Miniconf trait. We provide a default implementation for arrays in lib.rs that is non-atomic. We can't implement Miniconf in both an atomic and non-atomic manner for the same type (since atomic and non-atomic both use the Miniconf trait), so I think the only way we could support this is by augmenting the type of the array.

jordens commented 2 years ago

The derive macro would defer to different implementations of the Miniconf trait depending on the attributes.

jordens commented 2 years ago

I mean, if the update is atomic, there is no need to implement Miniconf for the array member anyway.

ryan-summers commented 2 years ago

To clarify what I mean, Miniconf is implemented automatically for all arrays of types that impl Miniconf here: https://github.com/quartiq/miniconf/blob/develop/src/array.rs

We don't dynamically "select" an implementation in the proc-macro and I don't know if that's possible. We could do it with cfgs and feature flags, but it'd be an all-or-nothing approach.

jordens commented 2 years ago

We are generating code to dispatch into the member string_set(). Either generate different code for struct members that have the atomic attribute, i.e. string_set_atomic() or generate something like self.member = Member::deserialize(...).