Skip to content

Commit db86b74

Browse files
committed
Add Progress::take(), refactor Progress as generics
1 parent a2aaed1 commit db86b74

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

examples/xbd-net/src/xbd/gcoap.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ impl Future for Req {
111111
//
112112

113113
#[derive(Debug)]// !!!! V1
114-
pub struct Progress(Option<AtomicWaker>, pub Option<AtomicWaker>, pub Option<GcoapMemoState>);
114+
pub struct Progress<T>(Option<AtomicWaker>, pub Option<AtomicWaker>, pub Option<T>);
115115

116-
impl Progress {
116+
impl<T> Progress<T> {
117117
pub fn new() -> Self {
118118
Self(Some(AtomicWaker::new()), None, None)
119119
}
@@ -130,18 +130,24 @@ impl Progress {
130130
self.1.replace(waker);
131131
}
132132

133-
pub fn finish(&mut self, memo: GcoapMemoState) {
133+
pub fn resolve(&mut self, ret: T) {
134134
assert!(self.0.is_none() && self.1.is_some() && self.2.is_none()); // registered
135135

136-
self.2.replace(memo);
136+
self.2.replace(ret);
137137
self.1.take().unwrap().wake();
138138
}
139139

140+
pub fn take(&mut self) -> T {
141+
assert!(self.0.is_none() && self.1.is_none() && self.2.is_some()); // resolved
142+
143+
self.2.take().unwrap()
144+
}
145+
140146
pub fn as_mut_ptr(&self) -> *mut Self {
141147
self as *const _ as *mut _
142148
}
143149

144-
pub fn get_ref_mut(ptr: *mut Self) -> &'static mut Self {
150+
pub fn get_mut_ref(ptr: *mut Self) -> &'static mut Self {
145151
unsafe { &mut *ptr }
146152
}
147153
}
@@ -155,7 +161,7 @@ pub struct ReqInner {
155161
blockwise: bool,
156162
blockwise_state_index: Option<usize>,
157163
blockwise_hdr: Option<heapless::Vec<u8, BLOCKWISE_HDR_MAX>>,
158-
progress: Progress,
164+
progress: Progress<GcoapMemoState>,
159165
}
160166

161167
impl ReqInner {
@@ -177,12 +183,12 @@ impl ReqInner {
177183
}
178184
}
179185

180-
fn gcoap_get(addr: &str, uri: &str, progress_ptr: *mut Progress) {
186+
fn gcoap_get(addr: &str, uri: &str, progress_ptr: *mut Progress<GcoapMemoState>) {
181187
super::Xbd::gcoap_req_v2(addr, uri, COAP_METHOD_GET, None, false,
182188
None, progress_ptr);
183189
}
184190

185-
fn gcoap_get_blockwise(addr: &str, uri: &str, blockwise_state_index: usize, progress_ptr: *mut Progress) {
191+
fn gcoap_get_blockwise(addr: &str, uri: &str, blockwise_state_index: usize, progress_ptr: *mut Progress<GcoapMemoState>) {
186192
super::Xbd::gcoap_req_v2(addr, uri, COAP_METHOD_GET, None, true,
187193
Some(blockwise_state_index), progress_ptr);
188194
}
@@ -239,8 +245,7 @@ impl Future for ReqInner {
239245

240246
Poll::Pending
241247
} else {
242-
let out = self.progress.2.take().unwrap();
243-
Poll::Ready(out)
248+
Poll::Ready(self.progress.take())
244249
}
245250
}
246251
}

examples/xbd-net/src/xbd/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl Xbd {
108108
// TODO move to 'gcoap.rs'
109109
fn gcoap_req_v2(addr: &str, uri: &str, method: gcoap::CoapMethod,
110110
payload: Option<&[u8]>, blockwise: bool, blockwise_state_index: Option<usize>,
111-
progress_ptr: *mut Progress) {
111+
progress_ptr: *mut Progress<GcoapMemoState>) {
112112
let payload_ptr = payload.map_or(core::ptr::null(), |payload| payload.as_ptr());
113113
let payload_len = payload.map_or(0, |payload| payload.len());
114114

@@ -159,7 +159,7 @@ impl Xbd {
159159
};
160160

161161
let memo = GcoapMemoState::new(memo_state, payload);
162-
Progress::get_ref_mut(context as *mut _).finish(memo);
162+
Progress::get_mut_ref(context as *mut _).resolve(memo);
163163
}
164164

165165
pub fn async_sleep(msec: u32) -> impl Future<Output = ()> + 'static {

0 commit comments

Comments
 (0)