Skip to content

Commit 2af5734

Browse files
committed
as_ffi_pair returns a raw pointer
1 parent a2252ce commit 2af5734

File tree

2 files changed

+14
-37
lines changed

2 files changed

+14
-37
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
8686
issues. No longer allows creating a `UnixAddr` from a raw `sockaddr_un`.
8787
([#1496](https://github.com/nix-rust/nix/pull/1496))
8888

89+
- Changed `SockAddr::as_ffi_pair` to return a raw pointer to the `sockaddr`
90+
instead of a reference.
91+
(#[1504](https://github.com/nix-rust/nix/pull/1504))
92+
8993
### Fixed
9094

9195
- Added more errno definitions for better backwards compatibility with

src/sys/socket/addr.rs

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -907,60 +907,39 @@ impl SockAddr {
907907
/// with the size of the actual data type. sockaddr is commonly used as a proxy for
908908
/// a superclass as C doesn't support inheritance, so many functions that take
909909
/// a sockaddr * need to take the size of the underlying type as well and then internally cast it back.
910-
pub fn as_ffi_pair(&self) -> (&libc::sockaddr, libc::socklen_t) {
910+
pub fn as_ffi_pair(&self) -> (*const libc::sockaddr, libc::socklen_t) {
911911
match *self {
912912
SockAddr::Inet(InetAddr::V4(ref addr)) => (
913-
// This cast is always allowed in C
914-
unsafe {
915-
&*(addr as *const libc::sockaddr_in as *const libc::sockaddr)
916-
},
913+
addr as *const libc::sockaddr_in as *const libc::sockaddr,
917914
mem::size_of_val(addr) as libc::socklen_t
918915
),
919916
SockAddr::Inet(InetAddr::V6(ref addr)) => (
920-
// This cast is always allowed in C
921-
unsafe {
922-
&*(addr as *const libc::sockaddr_in6 as *const libc::sockaddr)
923-
},
917+
addr as *const libc::sockaddr_in6 as *const libc::sockaddr,
924918
mem::size_of_val(addr) as libc::socklen_t
925919
),
926920
SockAddr::Unix(UnixAddr { ref sun, path_len }) => (
927-
// This cast is always allowed in C
928-
unsafe {
929-
&*(sun as *const libc::sockaddr_un as *const libc::sockaddr)
930-
},
921+
sun as *const libc::sockaddr_un as *const libc::sockaddr,
931922
(path_len + offset_of!(libc::sockaddr_un, sun_path)) as libc::socklen_t
932923
),
933924
#[cfg(any(target_os = "android", target_os = "linux"))]
934925
SockAddr::Netlink(NetlinkAddr(ref sa)) => (
935-
// This cast is always allowed in C
936-
unsafe {
937-
&*(sa as *const libc::sockaddr_nl as *const libc::sockaddr)
938-
},
926+
sa as *const libc::sockaddr_nl as *const libc::sockaddr,
939927
mem::size_of_val(sa) as libc::socklen_t
940928
),
941929
#[cfg(any(target_os = "android", target_os = "linux"))]
942930
SockAddr::Alg(AlgAddr(ref sa)) => (
943-
// This cast is always allowed in C
944-
unsafe {
945-
&*(sa as *const libc::sockaddr_alg as *const libc::sockaddr)
946-
},
931+
sa as *const libc::sockaddr_alg as *const libc::sockaddr,
947932
mem::size_of_val(sa) as libc::socklen_t
948933
),
949934
#[cfg(any(target_os = "ios", target_os = "macos"))]
950935
SockAddr::SysControl(SysControlAddr(ref sa)) => (
951-
// This cast is always allowed in C
952-
unsafe {
953-
&*(sa as *const libc::sockaddr_ctl as *const libc::sockaddr)
954-
},
936+
sa as *const libc::sockaddr_ctl as *const libc::sockaddr,
955937
mem::size_of_val(sa) as libc::socklen_t
956938

957939
),
958940
#[cfg(any(target_os = "android", target_os = "linux"))]
959941
SockAddr::Link(LinkAddr(ref addr)) => (
960-
// This cast is always allowed in C
961-
unsafe {
962-
&*(addr as *const libc::sockaddr_ll as *const libc::sockaddr)
963-
},
942+
addr as *const libc::sockaddr_ll as *const libc::sockaddr,
964943
mem::size_of_val(addr) as libc::socklen_t
965944
),
966945
#[cfg(any(target_os = "dragonfly",
@@ -971,18 +950,12 @@ impl SockAddr {
971950
target_os = "netbsd",
972951
target_os = "openbsd"))]
973952
SockAddr::Link(LinkAddr(ref addr)) => (
974-
// This cast is always allowed in C
975-
unsafe {
976-
&*(addr as *const libc::sockaddr_dl as *const libc::sockaddr)
977-
},
953+
addr as *const libc::sockaddr_dl as *const libc::sockaddr,
978954
mem::size_of_val(addr) as libc::socklen_t
979955
),
980956
#[cfg(any(target_os = "android", target_os = "linux"))]
981957
SockAddr::Vsock(VsockAddr(ref sa)) => (
982-
// This cast is always allowed in C
983-
unsafe {
984-
&*(sa as *const libc::sockaddr_vm as *const libc::sockaddr)
985-
},
958+
sa as *const libc::sockaddr_vm as *const libc::sockaddr,
986959
mem::size_of_val(sa) as libc::socklen_t
987960
),
988961
}

0 commit comments

Comments
 (0)