For context: I am trying to write a Rust wrapper over a C library.

Like many C libraries, most of its functions return an int. Positive return values are meaningful (provides information) and negative values are error codes.

To give an example, think of something like int get_items_from_record(const struct record *rec, struct item *items). A positive value indicates how many items were returned. -1 could mean ErrorA, -2 ErrorB, and so on.

Since this is Rust, I want to represent this kind of integer as Result<T, E>, e.g.:

enum LibError {
    A = -1,
    B = -2,
    // ....
}

// LibResult is ideally just represented as an integer.
type LibResult = Result<NonNegativeInteger, LibError>;

// Then I can pass LibResult values back to the C code as i32 trivially.

Is there a way/crate to do this?

  • RunAwayFrog@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    4
    arrow-down
    1
    ·
    edit-2
    1 year ago

    Discriminant is irrelevant and you’re not supposed to fuck with it.

    And there is zero reason to use unsafe/transmute for this.

    pub enum LibErr {
        ErrX,
        ErrY,
        ErrZ,
        Unknown(i32),
    }
    
    struct RetVal(i32);
    
    impl From for Result {
        fn from(RetVal(ret_val): RetVal) -> Self {
            if ret_val &lt; 0 {
                match ret_val {
                    -1 => Err(LibErr::ErrX),
                    -2 => Err(LibErr::ErrY),
                    -3 => Err(LibErr::ErrZ),
                    unknown => Err(LibErr::Unknown(unknown)),
                }
            } else { Ok(ret_val) }
        }
    }
    
    // RetVal(call_function()).into()
    
    • donnachaidh@lemmy.dcmrobertson.com
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      And just to explicitly point out, your code’s also better because of the use of the standard traits. It took me a while to get into the habit, but using what’s already there is always a good idea.

    • Aloso@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      Discriminant is irrelevant and you’re not supposed to fuck with it

      It matters because the conversion between i32 and the Result is only “free” if they have the same layout (which they do not, because of the discriminant). So a more costly conversion method is required.

      And there is zero reason to use unsafe/transmute for this.

      You are right, because the compiler is able to optimize your code quite well. However, if that optimization were to break at some point (as there is no guarantee that an optimization will continue to work in the future), it would become less efficient.

      • donnachaidh@lemmy.dcmrobertson.com
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        That seems like strong premature optimisation. Perhaps worth a note, but I’d presume the majority of people the majority of the time wouldn’t need to worry about that.