Skip to content

Commit

Permalink
chore(net): add type constraint for get/set_socket_option
Browse files Browse the repository at this point in the history
  • Loading branch information
AsakuraMizu committed Aug 27, 2024
1 parent b1ed765 commit 148f3ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
18 changes: 14 additions & 4 deletions compio-net/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl Socket {
}

#[cfg(unix)]
pub unsafe fn get_socket_option<T>(&self, level: i32, name: i32) -> io::Result<T> {
pub unsafe fn get_socket_option<T: Copy>(&self, level: i32, name: i32) -> io::Result<T> {
let mut value: MaybeUninit<T> = MaybeUninit::uninit();
let mut len = size_of::<T>() as libc::socklen_t;
syscall!(libc::getsockopt(
Expand All @@ -342,7 +342,7 @@ impl Socket {
}

#[cfg(windows)]
pub unsafe fn get_socket_option<T>(&self, level: i32, name: i32) -> io::Result<T> {
pub unsafe fn get_socket_option<T: Copy>(&self, level: i32, name: i32) -> io::Result<T> {
let mut value: MaybeUninit<T> = MaybeUninit::uninit();
let mut len = size_of::<T>() as i32;
syscall!(
Expand All @@ -363,7 +363,12 @@ impl Socket {
}

#[cfg(unix)]
pub unsafe fn set_socket_option<T>(&self, level: i32, name: i32, value: &T) -> io::Result<()> {
pub unsafe fn set_socket_option<T: Copy>(
&self,
level: i32,
name: i32,
value: &T,
) -> io::Result<()> {
syscall!(libc::setsockopt(
self.socket.as_raw_fd(),
level,
Expand All @@ -375,7 +380,12 @@ impl Socket {
}

#[cfg(windows)]
pub unsafe fn set_socket_option<T>(&self, level: i32, name: i32, value: &T) -> io::Result<()> {
pub unsafe fn set_socket_option<T: Copy>(
&self,
level: i32,
name: i32,
value: &T,
) -> io::Result<()> {
syscall!(
SOCKET,
windows_sys::Win32::Networking::WinSock::setsockopt(
Expand Down
9 changes: 7 additions & 2 deletions compio-net/src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ impl UdpSocket {
/// # Safety
///
/// The caller must ensure `T` is the correct type for `level` and `name`.
pub unsafe fn get_socket_option<T>(&self, level: i32, name: i32) -> io::Result<T> {
pub unsafe fn get_socket_option<T: Copy>(&self, level: i32, name: i32) -> io::Result<T> {
self.inner.get_socket_option(level, name)
}

Expand All @@ -330,7 +330,12 @@ impl UdpSocket {
/// # Safety
///
/// The caller must ensure `T` is the correct type for `level` and `name`.
pub unsafe fn set_socket_option<T>(&self, level: i32, name: i32, value: &T) -> io::Result<()> {
pub unsafe fn set_socket_option<T: Copy>(
&self,
level: i32,
name: i32,
value: &T,
) -> io::Result<()> {
self.inner.set_socket_option(level, name, value)
}
}
Expand Down

0 comments on commit 148f3ac

Please sign in to comment.