8000 Conversion to Box<dyn Error> loses downcast ability · Issue #379 · dtolnay/anyhow · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Conversion to Box<dyn Error> loses downcast ability #379
Closed
@seanmonstar

Description

@seanmonstar

When I have an anyhow::Error and convert that into a Box<dyn std::error::Error>, it seems that I can no longer downcast it back into anything (neither anyhow::Error nor the original error type). This differs from the behavior of casting the anyhow error to a &(dyn StdError + 'static), which does still allow downcasting to the orignal type.

Perhaps this is a limitation that cannot be solved, but I couldn't find it documented so I wasn't sure if it was known.

use std::fmt;
use std::error::Error as StdError;

type BoxError = Box<dyn StdError + Send + Sync>;

#[derive(Debug)]
struct Sentinel;

impl fmt::Display for Sentinel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("sentinel")
    }
}

impl StdError for Sentinel {}

let a: anyhow::Error = Sentinel.into();
let ar: &(dyn StdError + 'static) = a.as_ref();
dbg!(ar.is::<Sentinel>()); // true
let b: BoxError = a.into();
dbg!(b.is::<Sentinel>()); // false
//dbg!(b.is::<anyhow::Error>()); // can't do

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0