mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
Move SocketAddrCRepr to sys_common
This commit is contained in:
parent
7aabd85faa
commit
b20b69a79b
@ -596,37 +596,6 @@ impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A type with the same memory layout as `c::sockaddr`. Used in converting Rust level
|
|
||||||
/// SocketAddr* types into their system representation. The benefit of this specific
|
|
||||||
/// type over using `c::sockaddr_storage` is that this type is exactly as large as it
|
|
||||||
/// needs to be and not a lot larger. And it can be initialized more cleanly from Rust.
|
|
||||||
#[repr(C)]
|
|
||||||
pub(crate) union SocketAddrCRepr {
|
|
||||||
v4: c::sockaddr_in,
|
|
||||||
v6: c::sockaddr_in6,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SocketAddrCRepr {
|
|
||||||
pub fn as_ptr(&self) -> *const c::sockaddr {
|
|
||||||
self as *const _ as *const c::sockaddr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> IntoInner<(SocketAddrCRepr, c::socklen_t)> for &'a SocketAddr {
|
|
||||||
fn into_inner(self) -> (SocketAddrCRepr, c::socklen_t) {
|
|
||||||
match *self {
|
|
||||||
SocketAddr::V4(ref a) => {
|
|
||||||
let sockaddr = SocketAddrCRepr { v4: a.into_inner() };
|
|
||||||
(sockaddr, mem::size_of::<c::sockaddr_in>() as c::socklen_t)
|
|
||||||
}
|
|
||||||
SocketAddr::V6(ref a) => {
|
|
||||||
let sockaddr = SocketAddrCRepr { v6: a.into_inner() };
|
|
||||||
(sockaddr, mem::size_of::<c::sockaddr_in6>() as c::socklen_t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl fmt::Display for SocketAddr {
|
impl fmt::Display for SocketAddr {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
@ -700,3 +700,38 @@ impl fmt::Debug for UdpSocket {
|
|||||||
res.field(name, &self.inner.as_raw()).finish()
|
res.field(name, &self.inner.as_raw()).finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Converting SocketAddr to libc representation
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// A type with the same memory layout as `c::sockaddr`. Used in converting Rust level
|
||||||
|
/// SocketAddr* types into their system representation. The benefit of this specific
|
||||||
|
/// type over using `c::sockaddr_storage` is that this type is exactly as large as it
|
||||||
|
/// needs to be and not a lot larger. And it can be initialized more cleanly from Rust.
|
||||||
|
#[repr(C)]
|
||||||
|
pub(crate) union SocketAddrCRepr {
|
||||||
|
v4: c::sockaddr_in,
|
||||||
|
v6: c::sockaddr_in6,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SocketAddrCRepr {
|
||||||
|
pub fn as_ptr(&self) -> *const c::sockaddr {
|
||||||
|
self as *const _ as *const c::sockaddr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoInner<(SocketAddrCRepr, c::socklen_t)> for &'a SocketAddr {
|
||||||
|
fn into_inner(self) -> (SocketAddrCRepr, c::socklen_t) {
|
||||||
|
match *self {
|
||||||
|
SocketAddr::V4(ref a) => {
|
||||||
|
let sockaddr = SocketAddrCRepr { v4: a.into_inner() };
|
||||||
|
(sockaddr, mem::size_of::<c::sockaddr_in>() as c::socklen_t)
|
||||||
|
}
|
||||||
|
SocketAddr::V6(ref a) => {
|
||||||
|
let sockaddr = SocketAddrCRepr { v6: a.into_inner() };
|
||||||
|
(sockaddr, mem::size_of::<c::sockaddr_in6>() as c::socklen_t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user