/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Implements a simple oneshot channel. //! //! We used to use the `oneshot` crate for this, but the dependency was hard to manage //! (https://github.com/mozilla/uniffi-rs/issues/1736) //! //! This implementation is less efficient, but the difference is probably negligible for most //! use-cases involving UniFFI.
use std::{
future::Future,
pin::Pin,
sync::{Arc, Mutex},
task::{Context, Poll, Waker},
};
impl<T> Sender<T> { /// Send a value to the receiver pubfn send(self, value: T) { letmut inner = self.0.lock().unwrap();
inner.value = Some(value); iflet Some(waker) = inner.waker.take() {
waker.wake();
}
}
/// Convert a Sender into a raw pointer /// /// from_raw must be called with this pointer or else the sender will leak pubfn into_raw(self) -> *const () {
Arc::into_raw(self.0) as *const ()
}
/// Convert a raw pointer back to a Sender /// /// # Safety /// /// `raw_ptr` must have come from into_raw(). Once a pointer is passed into `from_raw` it must /// not be used again. pubunsafefn from_raw(raw_ptr: *const ()) -> Self { Self(Arc::from_raw(raw_ptr as *const Mutex<OneshotInner<T>>))
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.