Skip to content

Commit 16226d9

Browse files
committed
Add example for SockAddr::from_raw_sockaddr
1 parent bfc65a6 commit 16226d9

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/sys/socket/addr.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,23 @@ impl SockAddr {
808808
/// of the structure. Note that in the case of a `sockaddr_un`, `len` need not
809809
/// include the terminating null.
810810
///
811+
/// # Example
812+
/// ```no_run
813+
/// # fn main() -> nix::Result<()> {
814+
/// # use nix::sys::socket::SockAddr;
815+
/// use libc::{sockaddr, sockaddr_storage, socklen_t};
816+
/// extern "C" {
817+
/// fn get_me_an_address(addr: *mut sockaddr, addrlen: *mut socklen_t);
818+
/// }
819+
/// use std::mem;
820+
/// let mut addr = mem::MaybeUninit::<sockaddr_storage>::uninit();
821+
/// let mut addrlen = mem::size_of::<sockaddr_storage>() as socklen_t;
822+
/// unsafe { get_me_an_address(addr.as_mut_ptr() as *mut sockaddr, &mut addrlen) };
823+
/// let addr: SockAddr = unsafe { SockAddr::from_raw_sockaddr(addr.as_ptr() as *const sockaddr, addrlen as usize)? };
824+
/// # let _ = addr;
825+
/// # Ok(()) }
826+
/// ```
827+
///
811828
/// # Safety
812829
/// `addr` must be a valid, non-null pointer, and `len` should describe the
813830
/// number of bytes within `*addr` that are initialized and represent data.

0 commit comments

Comments
 (0)