there is "owning" task that periodically updates current_value and signals into is_high if necessary.
consumers only ever use is_high field that is thread safe.
However, rust won't allow sharing reference to this struct between owning producer task and various consumer tasks because pin and current_value are not thread safe.
How to work around this in an idiomatic way?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The idiomatic solution in Rust is to split your data into parts that are shared and parts that remain local. In your example, only the is_high field really needs to be shared between tasks, while the pin and any other transient state (like current_value) can stay with the owning (producer) task.
There are a couple of ways to do this:
Struct Splitting (Handle Pattern):
Instead of trying to share the entire GpioWithDebounce struct, you can restructure your code so that the producer owns a full struct that includes both the local (non-thread-safe) fields and the thread-safe parts. Then, it creates and passes out a handle (or channel, or other thread-safe primitive such as an Arc<AtomicBool>, etc.) that only gives access to the shared piece. In your case, that handle might just contain the is_high field. This “handle” is safe to share because it doesn’t include the non-thread-safe hardware abstraction.
For example, you might refactor your types like this:
rust
// This is the shared part that consumers can use:
#[derive(Clone)]
pub struct GpioSignal {
// You might wrap this in an Arc, AtomicBool, or your ThreadSafeSignal type.
pub is_high: ThreadSafeSignal<bool>,
}
// The full Gpio structure remains local to the producer task.
pub struct GpioWithDebounce {
pin: Input<'static>,
current_value: u32,
// The same thread-safe field is also stored here.
signal: GpioSignal,
}
impl GpioWithDebounce {
pub fn new(pin: Input<'static>) -> Self {
Self {
pin,
current_value: 0,
signal: GpioSignal {
is_high: ThreadSafeSignal::new(false),
},
}
}
// A method to return a cloneable handle for consumers.
pub fn handle(&self) -> GpioSignal {
self.signal.clone()
}
pub fn update(&mut self) {
// Do your polling, debounce logic, etc.
// And update `signal.is_high` if necessary.
}
}
In your embedded application, the producer task creates a GpioWithDebounce, does its local hardware manipulation and state updates, and then hands out clones of the GpioSignal (the handle) to any consumer that needs to be informed about the button/pin state.
Separate Communication Channels:
In some cases you might use a message-passing approach (for example, with RTIC or another RTOS framework) where the producer task sends events or signals via a channel/mailbox, and the consumers receive them. Again, the key idea is that the hardware-specific parts of the struct remain in one task, and only the thread-safe “signal” is communicated.
Using Arc with Interior Mutability:
If your thread-safe signal is something like an Arc<AtomicBool> or built upon a type that uses interior mutability (like a Mutex or Cell that’s designed for concurrent access), you can share only that piece. You’ll still need to ensure that non-thread-safe pieces (like the hardware pin and other transient state) are not accessible from multiple tasks.
Why This Matters
Rust ensures thread safety by enforcing that only types that implement the Send (and often Sync) traits can be shared across threads. In your original struct:
the fields pin and current_value are not meant for concurrent access and likely don’t implement Send/Sync. Thus, sharing a reference that includes them leads to compile-time errors. By separating these fields from the part that must be shared, you satisfy the compiler’s guarantees about thread safety without compromising your design.
Summary
Do not try to share the entire struct: Split it into a producer part (with non-thread-safe fields) and a consumer handle (with only thread-safe fields).
Keep hardware access local: The task that owns the hardware (the pin) should also be responsible for updating local state.
Expose a safe interface: Only share a handle (like a cloned GpioSignal) that is fully thread-safe.
Consider channels: For many embedded or concurrent applications, message passing or signaling provides a natural boundary between tasks.
This pattern is both idiomatic in Rust and consistent with sound embedded design by ensuring that hardware interactions remain localized while still providing a thread-safe interface for the rest of your program.