Skip to content

Commit

Permalink
Add Progress::take(), refactor Progress as generics
Browse files Browse the repository at this point in the history
  • Loading branch information
j-devel committed Jun 17, 2024
1 parent a2aaed1 commit db86b74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 15 additions & 10 deletions examples/xbd-net/src/xbd/gcoap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ impl Future for Req {
//

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

impl Progress {
impl<T> Progress<T> {
pub fn new() -> Self {
Self(Some(AtomicWaker::new()), None, None)
}
Expand All @@ -130,18 +130,24 @@ impl Progress {
self.1.replace(waker);
}

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

self.2.replace(memo);
self.2.replace(ret);
self.1.take().unwrap().wake();
}

pub fn take(&mut self) -> T {
assert!(self.0.is_none() && self.1.is_none() && self.2.is_some()); // resolved

self.2.take().unwrap()
}

pub fn as_mut_ptr(&self) -> *mut Self {
self as *const _ as *mut _
}

pub fn get_ref_mut(ptr: *mut Self) -> &'static mut Self {
pub fn get_mut_ref(ptr: *mut Self) -> &'static mut Self {
unsafe { &mut *ptr }
}
}
Expand All @@ -155,7 +161,7 @@ pub struct ReqInner {
blockwise: bool,
blockwise_state_index: Option<usize>,
blockwise_hdr: Option<heapless::Vec<u8, BLOCKWISE_HDR_MAX>>,
progress: Progress,
progress: Progress<GcoapMemoState>,
}

impl ReqInner {
Expand All @@ -177,12 +183,12 @@ impl ReqInner {
}
}

fn gcoap_get(addr: &str, uri: &str, progress_ptr: *mut Progress) {
fn gcoap_get(addr: &str, uri: &str, progress_ptr: *mut Progress<GcoapMemoState>) {
super::Xbd::gcoap_req_v2(addr, uri, COAP_METHOD_GET, None, false,
None, progress_ptr);
}

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

Poll::Pending
} else {
let out = self.progress.2.take().unwrap();
Poll::Ready(out)
Poll::Ready(self.progress.take())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/xbd-net/src/xbd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Xbd {
// TODO move to 'gcoap.rs'
fn gcoap_req_v2(addr: &str, uri: &str, method: gcoap::CoapMethod,
payload: Option<&[u8]>, blockwise: bool, blockwise_state_index: Option<usize>,
progress_ptr: *mut Progress) {
progress_ptr: *mut Progress<GcoapMemoState>) {
let payload_ptr = payload.map_or(core::ptr::null(), |payload| payload.as_ptr());
let payload_len = payload.map_or(0, |payload| payload.len());

Expand Down Expand Up @@ -159,7 +159,7 @@ impl Xbd {
};

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

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

0 comments on commit db86b74

Please sign in to comment.