dtolnay/cxx

Preserve cause-chain of errors propagated from rust -> c++ -> rust

Aperta

#290 aperta il 9 set 2020

 (1 commento) (0 reazioni) (0 assegnatari)Rust (253 fork)batch import
help wantedneeds design

Metriche repository

Star
 (4472 stelle)
Metriche merge PR
 (Merge medio 8m) (3 PR mergiate in 30 g)

Descrizione

use anyhow::{anyhow, Result};

#[cxx::bridge]
mod ffi {
    extern "C" {
        include!("repro.h");
        fn cthing() -> Result<()>;
    }

    extern "Rust" {
        fn rustthing() -> Result<()>;
    }
}

fn rustthing() -> Result<()> {
    Err(anyhow!("no such file or directory").context("failed to open: /nonexist"))
}

fn main() -> Result<()> {
    ffi::cthing()?;
    Ok(())
}
// repro.cc

void cthing() {
  rustthing();
}

This currently loses the cause chain and only prints:

Error: failed to open: /nonexist

Guida contributor