esp-hal icon indicating copy to clipboard operation
esp-hal copied to clipboard

Remove `new_with_config` constructors where possible

Open MabezDev opened this issue 1 year ago • 1 comments
trafficstars

I think it might just be cleaner to have new always take a config, users can always just pass Default::default().

Related to #2010 and #2321

MabezDev avatar Oct 22 '24 16:10 MabezDev

IMHO we should instead do new().with_config() but the compiler will first apply a default config so it's slightly less efficient... But I think I prefer the code bloat over the "required optional" parameter.

bugadani avatar Oct 22 '24 16:10 bugadani

IMHO we should instead do new().with_config() but the compiler will first apply a default config so it's slightly less efficient... But I think I prefer the code bloat over the "required optional" parameter.

I agree, but with a small tweak. It should be apply_config and it can be applied anytime. (Related: #2416 )

MabezDev avatar Oct 27 '24 23:10 MabezDev

We should probably put this in the API-GUIDELINES, but in general I think with_X makes sense on construction, and apply_ or other verb-based methods make sense post creation.

MabezDev avatar Oct 27 '24 23:10 MabezDev

re "the compiler will apply a default config" we can also choose to roll with hardware default initially. This point of view would better align with individual setters, where there are no default values defined by the HAL.

If we choose to go with individual setters, I'd probably argue for only keeping apply_X(&mut self) variants. While it's nice to chain-construct a peripheral, it's also pretty trivial to do that with mutating setters, which are more flexible.

bugadani avatar Oct 29 '24 14:10 bugadani

We should also think about what to do with SPI: currently we don't have an SpiDevice equivalent, which means we can't automatically reconfigure the bus for the devices' individual needs. A configuring version of ExclusiveDevice would align well with config structs and apply_config. So I guess there are pros and cons for each approach, the question is whether keeping a small API surface here is a good enough motivator to pick between them?

bugadani avatar Oct 29 '24 14:10 bugadani

We should also think about split peripherals: what does apply_config(uart::config::Config) mean for UartTx, what does it mean for UartRx? What does it mean to change the baud rate for half of a peripheral, for example?

bugadani avatar Oct 29 '24 14:10 bugadani

We should also think about split peripherals: what does apply_config(uart::config::Config) mean for UartTx, what does it mean for UartRx? What does it mean to change the baud rate for half of a peripheral, for example?

This is a good question, in embassy they treat it like a full apply on the whole Uart. I think I would also go down this route. Having to do Uart::unsplit(rx, tx).apply_config() or something like that would be quite terrible, for a number of reasons.

MabezDev avatar Oct 30 '24 11:10 MabezDev

I feel like this issue is made somewhat redundant by #2416

bugadani avatar Nov 22 '24 11:11 bugadani

I agree, I'll close this now.

MabezDev avatar Nov 22 '24 12:11 MabezDev