Skip to main content

pistonite_cu/
misc.rs

1use std::any::Any;
2
3use crate::Context as _;
4
5/// Try to get info from a panic payload
6pub fn best_effort_panic_info<'a>(payload: &'a Box<dyn Any + Send + 'static>) -> &'a str {
7    if let Some(s) = payload.downcast_ref::<&str>() {
8        s
9    } else if let Some(s) = payload.downcast_ref::<String>() {
10        s.as_str()
11    } else {
12        crate::debug!(
13            "encountered unknown panic info with type id: {:?}",
14            (**payload).type_id()
15        );
16        "unknown panic info"
17    }
18}
19
20/// Copy a reader to a writer.
21///
22/// This is wrapper for `std::io::copy` with error context
23#[inline(always)]
24pub fn copy<R, W>(reader: &mut R, writer: &mut W) -> crate::Result<u64>
25where
26    R: std::io::Read + ?Sized,
27    W: std::io::Write + ?Sized,
28{
29    std::io::copy(reader, writer).context("stream copy failed")
30}
31
32/// Copy an async reader to an async writer.
33///
34/// This is wrapper for `tokio::io::copy` with error context
35#[cfg(feature = "coroutine")]
36#[inline(always)]
37pub async fn co_copy<'a, R, W>(reader: &'a mut R, writer: &'a mut W) -> crate::Result<u64>
38where
39    R: tokio::io::AsyncRead + Unpin + ?Sized,
40    W: tokio::io::AsyncWrite + Unpin + ?Sized,
41{
42    tokio::io::copy(reader, writer)
43        .await
44        .context("async stream copy failed")
45}