asyncio/local/
dgram.rs

1use prelude::{Protocol, Endpoint};
2use ffi::{AF_UNIX, SOCK_DGRAM};
3use dgram_socket::DgramSocket;
4use local::{LocalProtocol, LocalEndpoint};
5
6use std::fmt;
7use std::mem;
8
9/// The datagram-oriented UNIX domain protocol.
10///
11/// # Example
12/// Create a server and client sockets.
13///
14/// ```rust,no_run
15/// use asyncio::{IoContext, Endpoint};
16/// use asyncio::local::{LocalDgram, LocalDgramEndpoint, LocalDgramSocket};
17///
18/// let ctx = &IoContext::new().unwrap();
19/// let ep = LocalDgramEndpoint::new("example.sock").unwrap();
20///
21/// let sv = LocalDgramSocket::new(ctx, LocalDgram).unwrap();
22/// sv.bind(&ep).unwrap();
23///
24/// let cl = LocalDgramSocket::new(ctx, ep.protocol()).unwrap();
25/// cl.connect(&ep).unwrap();
26/// ```
27#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
28pub struct LocalDgram;
29
30impl Protocol for LocalDgram {
31    type Endpoint = LocalEndpoint<Self>;
32
33    fn family_type(&self) -> i32 {
34        AF_UNIX
35    }
36
37    fn socket_type(&self) -> i32 {
38        SOCK_DGRAM
39    }
40
41    fn protocol_type(&self) -> i32 {
42        0
43    }
44
45    unsafe fn uninitialized(&self) -> Self::Endpoint {
46        mem::uninitialized()
47    }
48}
49
50impl LocalProtocol for LocalDgram {
51    type Socket = DgramSocket<Self>;
52}
53
54impl Endpoint<LocalDgram> for LocalEndpoint<LocalDgram> {
55    fn protocol(&self) -> LocalDgram {
56        LocalDgram
57    }
58}
59
60impl fmt::Debug for LocalDgram {
61    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62        write!(f, "LocalDgram")
63    }
64}
65
66impl fmt::Debug for LocalEndpoint<LocalDgram> {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        write!(f, "LocalEndpoint(Dgram:\"{}\")", self)
69    }
70}
71
72/// The datagram-oriented UNIX domain endpoint type.
73pub type LocalDgramEndpoint = LocalEndpoint<LocalDgram>;
74
75/// The datagram-oriented UNIX domain socket type.
76pub type LocalDgramSocket = DgramSocket<LocalDgram>;
77
78#[test]
79fn test_dgram() {
80    assert!(LocalDgram == LocalDgram);
81}
82
83#[test]
84fn test_format() {
85    use core::IoContext;
86
87    let ctx = &IoContext::new().unwrap();
88    println!("{:?}", LocalDgram);
89    println!("{:?}", LocalDgramEndpoint::new("foo/bar").unwrap());
90    println!("{:?}", LocalDgramSocket::new(ctx, LocalDgram).unwrap());
91}