Great question! This is a common pattern in embedded Rust, where you have a struct that owns some non-thread-safe resources (like a GPIO pin), but you want to share only a thread-safe "view" (like a signal) with other tasks.
Let's break down the problem and the idiomatic solutions:
Problem Recap
GpioWithDebounce owns a pin and a current_value (not thread-safe).
- It also owns a
ThreadSafeSignal<bool> (is_high), which is thread-safe.
- You want to share only
is_high with consumers, but Rust's ownership rules prevent you from sharing references to the whole struct.
Idiomatic Solutions
1. Split the Struct: Producer/Consumer Pattern
Producer owns the full struct (including the pin and current_value).
Consumers only get access to the thread-safe signal.
How?
- Make
is_high a separate, clonable, thread-safe type (e.g., Arc<Signal<bool>> or similar).
- The producer task owns the full struct.
- Consumers are given a clone of the signal.
Example:
use std::sync::Arc;
struct GpioWithDebounce {
pin: Input<'static>,
current_value: u32,
is_high: Arc<ThreadSafeSignal<bool>>,
}
impl GpioWithDebounce {
pub fn new(pin: Input<'static>) -> Self {
Self {
pin,
current_value: 0,
is_high: Arc::new(ThreadSafeSignal::new(false)),
}
}
pub fn signal_handle(&self) -> Arc<ThreadSafeSignal<bool>> {
self.is_high.clone()
}
}
// Producer task owns GpioWithDebounce
let gpio = GpioWithDebounce::new(...);
// Consumers only get the signal handle
let signal_handle = gpio.signal_handle();
// Pass `signal_handle` to consumer tasks
This is the most idiomatic and safe way.
You never share the whole struct, only the thread-safe part.
2. Encapsulation: Only Expose the Signal
If you want to keep the struct as is, do not share the struct.
Instead, provide a method to get a reference or clone of the signal.
impl GpioWithDebounce {
pub fn signal(&self) -> &ThreadSafeSignal<bool> {
&self.is_high
}
}
Or, if ThreadSafeSignal is clonable:
impl GpioWithDebounce {
pub fn signal_handle(&self) -> ThreadSafeSignal<bool> {
self.is_high.clone()
}
}
3. Use Interior Mutability for the Signal
If you need to mutate the signal from multiple places, ensure ThreadSafeSignal uses interior mutability (e.g., Mutex, AtomicBool, etc.), and only share that.
4. Don't Share the Whole Struct
Never share the whole struct between threads/tasks if it contains non-thread-safe fields.
Only share the thread-safe part.
Summary
- Do not share
&GpioWithDebounce or &mut GpioWithDebounce between tasks.
- Only share the thread-safe signal, via a clone or reference.
- Design your API so that consumers only ever see the signal, not the whole struct.
This is the idiomatic Rust way: split the producer/consumer responsibilities, and only share thread-safe handles.
Example in Practice
// Producer
let gpio = GpioWithDebounce::new(...);
// Consumer
let signal = gpio.signal_handle(); // Arc<ThreadSafeSignal<bool>>
spawn_consumer_task(signal);
If you show your ThreadSafeSignal type, I can give a more concrete example!
But the pattern above is the idiomatic Rust way to handle this.