use std::sync::Arc;
use crate::ffi::npp::context::Context;
use crate::runtime::Future;
pub struct Stream {
context: Arc<Context>,
}
impl Stream {
#[inline]
pub async fn null() -> Self {
let context = Future::new(Context::from_null_stream).await;
Self {
context: Arc::new(context),
}
}
#[inline]
pub async fn new() -> std::result::Result<Self, crate::Error> {
let stream = crate::Stream::new().await?;
let context = Future::new(move || Context::from_stream(stream)).await;
Ok(Self {
context: Arc::new(context),
})
}
pub(crate) fn to_context(&self) -> Arc<Context> {
self.context.clone()
}
}
impl std::ops::Deref for Stream {
type Target = crate::Stream;
fn deref(&self) -> &Self::Target {
&self.context.stream
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_new() {
let stream = Stream::new().await.unwrap();
assert!(!stream.to_context().as_ptr().is_null());
assert_eq!(
unsafe { *(stream.to_context().as_ptr() as *const *const std::ffi::c_void) },
stream.inner().as_internal().as_ptr(),
);
}
#[tokio::test]
async fn test_null() {
let stream = Stream::null().await;
assert!(!stream.to_context().as_ptr().is_null());
assert!(
unsafe { *(stream.to_context().as_ptr() as *const *const std::ffi::c_void) }.is_null()
);
}
}