add default data to watch new()

This commit is contained in:
Bronson 2024-11-10 12:50:11 +10:30
parent dc9fc73704
commit f9a1511de4

View File

@ -31,7 +31,7 @@ use crate::waitqueue::MultiWakerRegistration;
/// ///
/// let f = async { /// let f = async {
/// ///
/// static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); /// static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None);
/// ///
/// // Obtain receivers and sender /// // Obtain receivers and sender
/// let mut rcv0 = WATCH.receiver().unwrap(); /// let mut rcv0 = WATCH.receiver().unwrap();
@ -299,10 +299,10 @@ impl<M: RawMutex, T: Clone, const N: usize> WatchBehavior<T> for Watch<M, T, N>
impl<M: RawMutex, T: Clone, const N: usize> Watch<M, T, N> { impl<M: RawMutex, T: Clone, const N: usize> Watch<M, T, N> {
/// Create a new `Watch` channel. /// Create a new `Watch` channel.
pub const fn new() -> Self { pub const fn new(data: Option<T>) -> Self {
Self { Self {
mutex: Mutex::new(RefCell::new(WatchState { mutex: Mutex::new(RefCell::new(WatchState {
data: None, data,
current_id: 0, current_id: 0,
wakers: MultiWakerRegistration::new(), wakers: MultiWakerRegistration::new(),
receiver_count: 0, receiver_count: 0,
@ -775,7 +775,7 @@ mod tests {
#[test] #[test]
fn multiple_sends() { fn multiple_sends() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None);
// Obtain receiver and sender // Obtain receiver and sender
let mut rcv = WATCH.receiver().unwrap(); let mut rcv = WATCH.receiver().unwrap();
@ -801,7 +801,7 @@ mod tests {
#[test] #[test]
fn all_try_get() { fn all_try_get() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None);
// Obtain receiver and sender // Obtain receiver and sender
let mut rcv = WATCH.receiver().unwrap(); let mut rcv = WATCH.receiver().unwrap();
@ -835,7 +835,7 @@ mod tests {
static CONFIG0: u8 = 10; static CONFIG0: u8 = 10;
static CONFIG1: u8 = 20; static CONFIG1: u8 = 20;
static WATCH: Watch<CriticalSectionRawMutex, &'static u8, 1> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, &'static u8, 1> = Watch::new(None);
// Obtain receiver and sender // Obtain receiver and sender
let mut rcv = WATCH.receiver().unwrap(); let mut rcv = WATCH.receiver().unwrap();
@ -867,7 +867,7 @@ mod tests {
#[test] #[test]
fn sender_modify() { fn sender_modify() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None);
// Obtain receiver and sender // Obtain receiver and sender
let mut rcv = WATCH.receiver().unwrap(); let mut rcv = WATCH.receiver().unwrap();
@ -894,7 +894,7 @@ mod tests {
#[test] #[test]
fn predicate_fn() { fn predicate_fn() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None);
// Obtain receiver and sender // Obtain receiver and sender
let mut rcv = WATCH.receiver().unwrap(); let mut rcv = WATCH.receiver().unwrap();
@ -923,7 +923,7 @@ mod tests {
#[test] #[test]
fn receive_after_create() { fn receive_after_create() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None);
// Obtain sender and send value // Obtain sender and send value
let snd = WATCH.sender(); let snd = WATCH.sender();
@ -939,7 +939,7 @@ mod tests {
#[test] #[test]
fn max_receivers_drop() { fn max_receivers_drop() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None);
// Try to create 3 receivers (only 2 can exist at once) // Try to create 3 receivers (only 2 can exist at once)
let rcv0 = WATCH.receiver(); let rcv0 = WATCH.receiver();
@ -964,7 +964,7 @@ mod tests {
#[test] #[test]
fn multiple_receivers() { fn multiple_receivers() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None);
// Obtain receivers and sender // Obtain receivers and sender
let mut rcv0 = WATCH.receiver().unwrap(); let mut rcv0 = WATCH.receiver().unwrap();
@ -989,7 +989,7 @@ mod tests {
fn clone_senders() { fn clone_senders() {
let f = async { let f = async {
// Obtain different ways to send // Obtain different ways to send
static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 1> = Watch::new(None);
let snd0 = WATCH.sender(); let snd0 = WATCH.sender();
let snd1 = snd0.clone(); let snd1 = snd0.clone();
@ -1010,7 +1010,7 @@ mod tests {
#[test] #[test]
fn use_dynamics() { fn use_dynamics() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None);
// Obtain receiver and sender // Obtain receiver and sender
let mut anon_rcv = WATCH.dyn_anon_receiver(); let mut anon_rcv = WATCH.dyn_anon_receiver();
@ -1031,7 +1031,7 @@ mod tests {
#[test] #[test]
fn convert_to_dyn() { fn convert_to_dyn() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None);
// Obtain receiver and sender // Obtain receiver and sender
let anon_rcv = WATCH.anon_receiver(); let anon_rcv = WATCH.anon_receiver();
@ -1057,7 +1057,7 @@ mod tests {
#[test] #[test]
fn dynamic_receiver_count() { fn dynamic_receiver_count() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None);
// Obtain receiver and sender // Obtain receiver and sender
let rcv0 = WATCH.receiver(); let rcv0 = WATCH.receiver();
@ -1087,7 +1087,7 @@ mod tests {
#[test] #[test]
fn contains_value() { fn contains_value() {
let f = async { let f = async {
static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(); static WATCH: Watch<CriticalSectionRawMutex, u8, 2> = Watch::new(None);
// Obtain receiver and sender // Obtain receiver and sender
let rcv = WATCH.receiver().unwrap(); let rcv = WATCH.receiver().unwrap();