1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std
::cell
::RefCell
;
14 use std
::net
::{ToSocketAddrs, UdpSocket}
;
20 /// An "in progress" UDP socket which has not yet been connected.
22 /// Allows configuration of a socket before the socket is connected.
23 pub struct UdpBuilder
{
24 socket
: RefCell
<Option
<Socket
>>,
28 /// Constructs a new UdpBuilder with the `AF_INET` domain, the `SOCK_DGRAM`
29 /// type, and with a protocol argument of 0.
31 /// Note that passing other kinds of flags or arguments can be done through
32 /// the `FromRaw{Fd,Socket}` implementation.
33 pub fn new_v4() -> io
::Result
<UdpBuilder
> {
34 Socket
::new(c
::AF_INET
, c
::SOCK_DGRAM
).map(::FromInner
::from_inner
)
37 /// Constructs a new UdpBuilder with the `AF_INET6` domain, the `SOCK_DGRAM`
38 /// type, and with a protocol argument of 0.
40 /// Note that passing other kinds of flags or arguments can be done through
41 /// the `FromRaw{Fd,Socket}` implementation.
42 pub fn new_v6() -> io
::Result
<UdpBuilder
> {
43 Socket
::new(c
::AF_INET6
, c
::SOCK_DGRAM
).map(::FromInner
::from_inner
)
46 /// Binds this socket to the specified address.
48 /// This function directly corresponds to the bind(2) function on Windows
50 pub fn bind
<T
>(&self, addr
: T
) -> io
::Result
<UdpSocket
>
51 where T
: ToSocketAddrs
53 try
!(self.with_socket(|sock
| {
54 let addr
= try
!(::one_addr(addr
));
57 Ok(self.socket
.borrow_mut().take().unwrap().into_inner().into_udp_socket())
60 fn with_socket
<F
>(&self, f
: F
) -> io
::Result
<()>
61 where F
: FnOnce(&Socket
) -> io
::Result
<()>
63 match *self.socket
.borrow() {
65 None
=> Err(io
::Error
::new(io
::ErrorKind
::Other
,
66 "builder has already finished its socket")),
71 impl fmt
::Debug
for UdpBuilder
{
72 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
73 write
!(f
, "UdpBuilder {{ socket: {:?} }}",
74 self.socket
.borrow().as_ref().unwrap())
78 impl ::AsInner
for UdpBuilder
{
79 type Inner
= RefCell
<Option
<Socket
>>;
80 fn as_inner(&self) -> &RefCell
<Option
<Socket
>> { &self.socket }
83 impl ::FromInner
for UdpBuilder
{
85 fn from_inner(sock
: Socket
) -> UdpBuilder
{
86 UdpBuilder { socket: RefCell::new(Some(sock)) }