Skip to content

Commit

Permalink
Rust: Migrate to use ffi QUIC_API_TABLE (#4756)
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu authored Jan 27, 2025
1 parent 5af9a3e commit 0b07410
Show file tree
Hide file tree
Showing 3 changed files with 495 additions and 505 deletions.
38 changes: 13 additions & 25 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,34 +102,24 @@ impl Error {
}

/// Convert from raw ffi error type.
pub fn from_raw(ec: QUIC_ERROR) -> Self {
Self(ec)
}

/// Temp api to use in manually written ffi code which is going to be
/// removed and replaced by auto generated ffi code.
/// This api will be replaced by from_raw.
pub fn from_u32(ec: u32) -> Self {
Self(ec as QUIC_ERROR)
pub fn ok_from_raw(ec: QUIC_ERROR) -> Result<(), Self> {
let e = Self(ec);
if e.is_ok() {
Ok(())
} else {
Err(e)
}
}

/// Return Err if the error is considered a failure.
/// Ok includes both "no error" and "pending" status codes.
pub fn ok(self) -> Result<(), Self> {
pub fn is_ok(&self) -> bool {
// on windows it is signed.
#[cfg(target_os = "windows")]
if self.0 < 0 {
Err(self)
} else {
Ok(())
}
return self.0 >= 0;

#[cfg(not(target_os = "windows"))]
if (self.0 as i32) > 0 {
Err(self)
} else {
Ok(())
}
return self.0 as i32 <= 0;
}
}

Expand Down Expand Up @@ -201,10 +191,8 @@ mod tests {

#[test]
fn error_ok_test() {
assert!(Error::new(ErrorCode::QUIC_ERROR_ABORTED).ok().is_err());
assert!(Error::new(ErrorCode::QUIC_ERROR_SUCCESS).ok().is_ok());
assert!(Error::from_raw(ErrorCode::QUIC_ERROR_PENDING as QUIC_ERROR)
.ok()
.is_ok());
assert!(!Error::new(ErrorCode::QUIC_ERROR_ABORTED).is_ok());
assert!(Error::new(ErrorCode::QUIC_ERROR_SUCCESS).is_ok());
assert!(Error::ok_from_raw(ErrorCode::QUIC_ERROR_PENDING as QUIC_ERROR).is_ok());
}
}
6 changes: 6 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ include!("linux_bindings.rs");
pub type ADDRESS_FAMILY = u16;
#[cfg(target_os = "windows")]
include!("win_bindings.rs");

/// Temp type for casting manual ffi flags. To be removed eventually.
#[cfg(not(target_os = "windows"))]
pub type QuicFlag = u32;
#[cfg(target_os = "windows")]
pub type QuicFlag = i32;
Loading

0 comments on commit 0b07410

Please sign in to comment.