Skip to content

Commit

Permalink
Run fmt and clippy for rust code (#4712)
Browse files Browse the repository at this point in the history
  • Loading branch information
youyuanwu authored Dec 18, 2024
1 parent f8e9cec commit 62ecc16
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
- name: Install Cargo
if: runner.os == 'Linux'
run: curl https://sh.rustup.rs -sSf | sh -s -- -y
- name: Cargo fmt
run: cargo fmt --all -- --check
- name: Cargo clippy
run: cargo clippy --all-targets -- -D warnings
- name: Cargo build
run: cargo build --all ${{ matrix.features }}
- name: Cargo test
Expand Down
4 changes: 2 additions & 2 deletions scripts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT License.

use cmake::Config;
use std::path::Path;
use std::env;
use std::path::Path;

fn main() {
let path_extra = "lib";
Expand Down Expand Up @@ -38,7 +38,7 @@ fn main() {
"aarch64-apple-darwin" => config
.define("CMAKE_OSX_ARCHITECTURES", "arm64")
.define("CMAKE_OSX_DEPLOYMENT_TARGET", "14.5"),
_ => &mut config
_ => &mut config,
};

let dst = config.build();
Expand Down
145 changes: 65 additions & 80 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ pub type BOOLEAN = ::std::os::raw::c_uchar;

/// Family of an IP address.
pub type AddressFamily = u16;
#[allow(clippy::unnecessary_cast)]
pub const ADDRESS_FAMILY_UNSPEC: AddressFamily = c_types::AF_UNSPEC as u16;
#[allow(clippy::unnecessary_cast)]
pub const ADDRESS_FAMILY_INET: AddressFamily = c_types::AF_INET as u16;
#[allow(clippy::unnecessary_cast)]
pub const ADDRESS_FAMILY_INET6: AddressFamily = c_types::AF_INET6 as u16;

/// IPv4 address payload.
Expand Down Expand Up @@ -73,7 +76,7 @@ impl Addr {
Addr {
ipv4: sockaddr_in {
family,
port: port,
port,
addr,
zero: [0, 0, 0, 0, 0, 0, 0, 0],
},
Expand All @@ -92,7 +95,7 @@ impl Addr {
Addr {
ipv6: sockaddr_in6 {
family,
port: port,
port,
flow_info,
addr,
scope_id,
Expand Down Expand Up @@ -128,7 +131,7 @@ mod status {
pub const QUIC_STATUS_ALPN_NEG_FAILURE: u32 = 0x80410007;
pub const QUIC_STATUS_STREAM_LIMIT_REACHED: u32 = 0x80410008;
pub const QUIC_STATUS_ALPN_IN_USE: u32 = 0x80410009;
pub const QUIC_STATUS_CLOSE_NOTIFY: u32 = 0x80410100 | 0;
pub const QUIC_STATUS_CLOSE_NOTIFY: u32 = 0x80410100;
pub const QUIC_STATUS_BAD_CERTIFICATE: u32 = 0x80410100 | 42;
pub const QUIC_STATUS_UNSUPPORTED_CERTIFICATE: u32 = 0x80410100 | 43;
pub const QUIC_STATUS_REVOKED_CERTIFICATE: u32 = 0x80410100 | 44;
Expand All @@ -143,8 +146,8 @@ mod status {
#[cfg(target_os = "linux")]
mod status {
pub const QUIC_STATUS_SUCCESS: u32 = 0;
pub const QUIC_STATUS_PENDING: u32 = 0xFFFFFFFE; /// -2
pub const QUIC_STATUS_CONTINUE: u32 = 0xFFFFFFFF; /// -1
pub const QUIC_STATUS_PENDING: u32 = 0xFFFFFFFE; // -2
pub const QUIC_STATUS_CONTINUE: u32 = 0xFFFFFFFF; // -1
pub const QUIC_STATUS_OUT_OF_MEMORY: u32 = 12;
pub const QUIC_STATUS_INVALID_PARAMETER: u32 = 22;
pub const QUIC_STATUS_INVALID_STATE: u32 = 1;
Expand Down Expand Up @@ -183,8 +186,8 @@ mod status {
#[cfg(target_os = "macos")]
mod status {
pub const QUIC_STATUS_SUCCESS: u32 = 0;
pub const QUIC_STATUS_PENDING: u32 = 0xFFFFFFFE; /// -2
pub const QUIC_STATUS_CONTINUE: u32 = 0xFFFFFFFF; /// -1
pub const QUIC_STATUS_PENDING: u32 = 0xFFFFFFFE; // -2
pub const QUIC_STATUS_CONTINUE: u32 = 0xFFFFFFFF; // -1
pub const QUIC_STATUS_OUT_OF_MEMORY: u32 = 12;
pub const QUIC_STATUS_INVALID_PARAMETER: u32 = 22;
pub const QUIC_STATUS_INVALID_STATE: u32 = 1;
Expand Down Expand Up @@ -780,7 +783,7 @@ pub struct QuicTlsSecrets {
}

#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Default)]
pub struct Settings {
pub is_set_flags: u64,
pub max_bytes_per_key: u64,
Expand Down Expand Up @@ -1216,15 +1219,10 @@ struct ApiTable {
flags: SendFlags,
client_send_context: *const c_void,
) -> u32,
resumption_ticket_validation_complete: extern "C" fn(
connection: Handle,
result: BOOLEAN,
) -> u32,
certificate_validation_complete: extern "C" fn(
connection: Handle,
result: BOOLEAN,
tls_alert: TlsAlertCode
) -> u32,
resumption_ticket_validation_complete:
extern "C" fn(connection: Handle, result: BOOLEAN) -> u32,
certificate_validation_complete:
extern "C" fn(connection: Handle, result: BOOLEAN, tls_alert: TlsAlertCode) -> u32,
}

#[link(name = "msquic")]
Expand Down Expand Up @@ -1296,11 +1294,10 @@ impl From<&Vec<u8>> for Buffer {

impl From<&[u8]> for Buffer {
fn from(data: &[u8]) -> Buffer {
let buffer = Buffer {
Buffer {
length: data.len() as u32,
buffer: data.as_ptr() as *mut u8,
};
buffer
}
}
}

Expand All @@ -1321,38 +1318,8 @@ impl QuicPerformance {
}

impl Settings {
pub fn new() -> Settings {
Settings {
is_set_flags: 0,
max_bytes_per_key: 0,
handshake_idle_timeout_ms: 0,
idle_timeout_ms: 0,
mtu_discovery_search_complete_timeout_us: 0,
tls_client_max_send_buffer: 0,
tls_server_max_send_buffer: 0,
stream_recv_window_default: 0,
stream_recv_buffer_default: 0,
conn_flow_control_window: 0,
max_worker_queue_delay_us: 0,
max_stateless_operations: 0,
initial_window_packets: 0,
send_idle_timeout_ms: 0,
initiall_rtt_ms: 0,
max_ack_delay_ms: 0,
disconnect_timeout_ms: 0,
keep_alive_interval_ms: 0,
congestion_control_algorithm: 0,
peer_bidi_stream_count: 0,
peer_unidi_stream_count: 0,
max_binding_stateless_operations: 0,
stateless_operation_expiration_ms: 0,
minimum_mtu: 0,
maximum_mtu: 0,
other_flags: 0,
mtu_operations_per_drain: 0,
mtu_discovery_missing_probe_count: 0,
dest_cid_update_idle_timeout_ms: 0,
}
pub fn new() -> Self {
Self::default()
}
pub fn set_peer_bidi_stream_count(&mut self, value: u16) -> &mut Settings {
self.is_set_flags |= 0x40000;
Expand Down Expand Up @@ -1483,7 +1450,7 @@ impl Configuration {
let context: *const c_void = ptr::null();
let new_configuration: Handle = ptr::null();
let mut settings_size: u32 = 0;
if settings != ptr::null() {
if !settings.is_null() {
settings_size = ::std::mem::size_of::<Settings>() as u32;
}
let status = unsafe {
Expand All @@ -1508,7 +1475,7 @@ impl Configuration {

pub fn load_credential(&self, cred_config: &CredentialConfig) -> Result<(), u32> {
let status =
unsafe { ((*self.table).configuration_load_credential)(self.handle, *&cred_config) };
unsafe { ((*self.table).configuration_load_credential)(self.handle, cred_config) };
if Status::failed(status) {
return Err(status);
}
Expand Down Expand Up @@ -1552,14 +1519,19 @@ impl Connection {
Ok(())
}

pub fn start(&self, configuration: &Configuration, server_name: &str, server_port: u16) -> Result<(), u32> {
pub fn start(
&self,
configuration: &Configuration,
server_name: &str,
server_port: u16,
) -> Result<(), u32> {
let server_name_safe = std::ffi::CString::new(server_name).unwrap();
let status = unsafe {
((*self.table).connection_start)(
self.handle,
configuration.handle,
0,
server_name_safe.as_ptr() as *const i8,
server_name_safe.as_ptr(),
server_port,
)
};
Expand Down Expand Up @@ -1660,7 +1632,7 @@ impl Connection {
let status = unsafe {
((*self.table).datagram_send)(
self.handle,
*&buffer,
buffer,
buffer_count,
flags,
client_send_context,
Expand All @@ -1672,16 +1644,9 @@ impl Connection {
Ok(())
}

pub fn resumption_ticket_validation_complete(
&self,
result: BOOLEAN,
) -> Result<(), u32> {
let status = unsafe {
((*self.table).resumption_ticket_validation_complete)(
self.handle,
result,
)
};
pub fn resumption_ticket_validation_complete(&self, result: BOOLEAN) -> Result<(), u32> {
let status =
unsafe { ((*self.table).resumption_ticket_validation_complete)(self.handle, result) };
if Status::failed(status) {
return Err(status);
}
Expand All @@ -1694,11 +1659,7 @@ impl Connection {
tls_alert: TlsAlertCode,
) -> Result<(), u32> {
let status = unsafe {
((*self.table).certificate_validation_complete)(
self.handle,
result,
tls_alert,
)
((*self.table).certificate_validation_complete)(self.handle, result, tls_alert)
};
if Status::failed(status) {
return Err(status);
Expand Down Expand Up @@ -1744,7 +1705,7 @@ impl Listener {
self.handle,
alpn.as_ptr(),
alpn.len() as u32,
*&local_address,
local_address,
)
};
if Status::failed(status) {
Expand Down Expand Up @@ -1822,7 +1783,7 @@ impl Stream {
let status = unsafe {
((*self.table).stream_send)(
self.handle,
*&buffer,
buffer,
buffer_count,
flags,
client_send_context, //(self as *const Stream) as *const c_void,
Expand Down Expand Up @@ -1915,11 +1876,19 @@ extern "C" fn test_stream_callback(
#[test]
fn test_module() {
let res = Api::new();
assert!(res.is_ok(), "Failed to open API: 0x{:x}", res.err().unwrap());
assert!(
res.is_ok(),
"Failed to open API: 0x{:x}",
res.err().unwrap()
);
let api = res.unwrap();

let res = Registration::new(&api, ptr::null());
assert!(res.is_ok(), "Failed to open registration: 0x{:x}", res.err().unwrap());
assert!(
res.is_ok(),
"Failed to open registration: 0x{:x}",
res.err().unwrap()
);
let registration = res.unwrap();

let alpn = [Buffer::from("h3")];
Expand All @@ -1930,23 +1899,39 @@ fn test_module() {
.set_peer_bidi_stream_count(100)
.set_peer_unidi_stream_count(3),
);
assert!(res.is_ok(), "Failed to open configuration: 0x{:x}", res.err().unwrap());
assert!(
res.is_ok(),
"Failed to open configuration: 0x{:x}",
res.err().unwrap()
);
let configuration = res.unwrap();

let cred_config = CredentialConfig::new_client();
let res = configuration.load_credential(&cred_config);
assert!(res.is_ok(), "Failed to load credential: 0x{:x}", res.err().unwrap());
assert!(
res.is_ok(),
"Failed to load credential: 0x{:x}",
res.err().unwrap()
);

let connection = Connection::new(&registration);
let res = connection.open(
&registration,
test_conn_callback,
&connection as *const Connection as *const c_void,
);
assert!(res.is_ok(), "Failed to open connection: 0x{:x}", res.err().unwrap());
assert!(
res.is_ok(),
"Failed to open connection: 0x{:x}",
res.err().unwrap()
);

let res = connection.start(&configuration, "www.cloudflare.com", 443);
assert!(res.is_ok(), "Failed to start connection: 0x{:x}", res.err().unwrap());
assert!(
res.is_ok(),
"Failed to start connection: 0x{:x}",
res.err().unwrap()
);

let duration = std::time::Duration::from_millis(1000);
std::thread::sleep(duration);
Expand Down

0 comments on commit 62ecc16

Please sign in to comment.