Debian Codegraph @

src/tcp.rs

raw symbols SCIP
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.
4 //
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.
10
11 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#;
12 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/;
13 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::{SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#, ToSocketAddrsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/socket_addr/ToSocketAddrs#, TcpListenerrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpListener#, TcpStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpStream#};
14 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
15
16 use IntoInnerrust-analyzer cargo net2 0.2.39 IntoInner#;
17 use socket::Socket;
18 use sys::c;
19
20 /// An "in progress" TCP socket which has not yet been connected or listened.
21 ///
22 /// Allows configuration of a socket before one of these operations is executed.
23 pub struct TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder# {
24 socketrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#socket.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Socketrust-analyzer cargo net2 0.2.39 socket/Socket#>>,
25 }
26
27 impl TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder# {
28 /// Constructs a new TcpBuilder with the `AF_INET` domain, the `SOCK_STREAM`
29 /// type, and with a protocol argument of 0.
30 ///
31 /// Note that passing other kinds of flags or arguments can be done through
32 /// the `FromRaw{Fd,Socket}` implementation.
33 pub fn new_v4rust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]new_v4().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#> {
34 Socketrust-analyzer cargo net2 0.2.39 socket/Socket#::newrust-analyzer cargo net2 0.2.39 socket/impl#[Socket]new().(crust-analyzer cargo net2 0.2.39 sys/c/::AF_INETrust-analyzer cargo libc 0.2.189 unix/linux_like/AF_INET., crust-analyzer cargo net2 0.2.39 sys/c/::SOCK_STREAMrust-analyzer cargo libc 0.2.189 unix/linux_like/linux/gnu/b64/x86_64/SOCK_STREAM.).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(::FromInnerrust-analyzer cargo net2 0.2.39 FromInner#::from_innerrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder][FromInner]from_inner().)
35 }
36
37 /// Constructs a new TcpBuilder with the `AF_INET6` domain, the `SOCK_STREAM`
38 /// type, and with a protocol argument of 0.
39 ///
40 /// Note that passing other kinds of flags or arguments can be done through
41 /// the `FromRaw{Fd,Socket}` implementation.
42 pub fn new_v6rust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]new_v6().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#> {
43 Socketrust-analyzer cargo net2 0.2.39 socket/Socket#::newrust-analyzer cargo net2 0.2.39 socket/impl#[Socket]new().(crust-analyzer cargo net2 0.2.39 sys/c/::AF_INET6rust-analyzer cargo libc 0.2.189 unix/linux_like/AF_INET6., crust-analyzer cargo net2 0.2.39 sys/c/::SOCK_STREAMrust-analyzer cargo libc 0.2.189 unix/linux_like/linux/gnu/b64/x86_64/SOCK_STREAM.).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(::FromInnerrust-analyzer cargo net2 0.2.39 FromInner#::from_innerrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder][FromInner]from_inner().)
44 }
45
46 /// Binds this socket to the specified address.
47 ///
48 /// This function directly corresponds to the bind(2) function on Windows
49 /// and Unix.
50 pub fn bindrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]bind().<T>(&self, addr: T) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<&TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#>
51 where T: ToSocketAddrsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/socket_addr/ToSocketAddrs#
52 {
53 self.with_socketrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]with_socket().(|sock| {
54 let addr = tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(::one_addrrust-analyzer cargo net2 0.2.39 one_addr().(addr));
55 sock.bindrust-analyzer cargo net2 0.2.39 socket/impl#[Socket]bind().(&addr)
56 }).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|()| self)
57 }
58
59 /// Mark a socket as ready to accept incoming connection requests using
60 /// accept()
61 ///
62 /// This function directly corresponds to the listen(2) function on Windows
63 /// and Unix.
64 ///
65 /// An error will be returned if `listen` or `connect` has already been
66 /// called on this builder.
67 pub fn listenrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]listen().(&self, backlog: i32) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<TcpListenerrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpListener#> {
68 self.with_socketrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]with_socket().(|sock| {
69 sock.listenrust-analyzer cargo net2 0.2.39 socket/impl#[Socket]listen().(backlog)
70 }).and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|()| {
71 self.to_tcp_listenerrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]to_tcp_listener().()
72 })
73 }
74
75 /// Initiate a connection on this socket to the specified address.
76 ///
77 /// This function directly corresponds to the connect(2) function on Windows
78 /// and Unix.
79 ///
80 /// An error will be returned if `listen` or `connect` has already been
81 /// called on this builder.
82 pub fn connectrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]connect().<T>(&self, addr: T) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<TcpStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpStream#>
83 where T: ToSocketAddrsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/socket_addr/ToSocketAddrs#
84 {
85 self.with_socketrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]with_socket().(|sock| {
86 let err = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#,
87 "no socket addresses resolved");
88 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(addr.to_socket_addrsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/socket_addr/ToSocketAddrs#to_socket_addrs().()).foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#fold().(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err), |prev, addr| {
89 prev.or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]or_else().(|_| sock.connectrust-analyzer cargo net2 0.2.39 socket/impl#[Socket]connect().(&addr))
90 })
91 }).and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|()| {
92 self.to_tcp_streamrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]to_tcp_stream().()
93 })
94 }
95
96 /// Converts this builder into a `TcpStream`
97 ///
98 /// This function will consume the internal socket and return it re-wrapped
99 /// as a `TcpStream`. An error will be returned if the internal socket has
100 /// already been consumed from a successful call to `connect`, `listen`,
101 /// etc.
102 pub fn to_tcp_streamrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]to_tcp_stream().(&self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<TcpStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpStream#> {
103 self.socketrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#socket..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().().takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|s| s.into_innerrust-analyzer cargo net2 0.2.39 socket/impl#[Socket][IntoInner]into_inner().().into_tcp_streamrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]into_tcp_stream().())
104 .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#,
105 "socket has already been consumed"))
106 }
107
108 /// Converts this builder into a `TcpListener`
109 ///
110 /// This function will consume the internal socket and return it re-wrapped
111 /// as a `TcpListener`. An error will be returned if the internal socket has
112 /// already been consumed from a successful call to `connect`, `listen`,
113 /// etc.
114 pub fn to_tcp_listenerrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]to_tcp_listener().(&self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<TcpListenerrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/tcp/TcpListener#> {
115 self.socketrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#socket..borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().().takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().()
116 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|s| s.into_innerrust-analyzer cargo net2 0.2.39 socket/impl#[Socket][IntoInner]into_inner().().into_tcp_listenerrust-analyzer cargo net2 0.2.39 sys/impl#[Socket]into_tcp_listener().())
117 .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#,
118 "socket has already been consumed"))
119 }
120
121 /// Returns the address of the local half of this TCP socket.
122 ///
123 /// An error will be returned if `listen` or `connect` has already been
124 /// called on this builder.
125 pub fn local_addrrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]local_addr().(&self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#> {
126 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Ref<'_, T>`][Deref]deref().self.socketrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#socket..borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().() {
127 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref s) => s.getsocknamerust-analyzer cargo net2 0.2.39 socket/impl#[Socket]getsockname().(),
128 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#,
129 "builder has already finished its socket")),
130 }
131 }
132
133 fn with_socketrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder]with_socket().<F>(&self, f: F) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()>
134 where F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&Socketrust-analyzer cargo net2 0.2.39 socket/Socket#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()>
135 {
136 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Ref<'_, T>`][Deref]deref().self.socketrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#socket..borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().() {
137 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref s) => f(s),
138 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#,
139 "builder has already finished its socket")),
140 }
141 }
142 }
143
144 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder# {
145 fn fmtrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
146 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "TcpBuilder {{ socket: {:?} }}",
147 self.socketrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#socket..borrowrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow().().as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().())
148 }
149 }
150
151 impl ::AsInnerrust-analyzer cargo net2 0.2.39 AsInner# for TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder# {
152 type Innerrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder][AsInner]Inner# = RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Socketrust-analyzer cargo net2 0.2.39 socket/Socket#>>;
153 fn as_innerrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder][AsInner]as_inner().(&self) -> &RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Socketrust-analyzer cargo net2 0.2.39 socket/Socket#>> { &self.socketrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#socket. }
154 }
155
156 impl ::FromInnerrust-analyzer cargo net2 0.2.39 FromInner# for TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder# {
157 type Innerrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder][FromInner]Inner# = Socketrust-analyzer cargo net2 0.2.39 socket/Socket#;
158 fn from_innerrust-analyzer cargo net2 0.2.39 tcp/impl#[TcpBuilder][FromInner]from_inner().(sock: Socketrust-analyzer cargo net2 0.2.39 socket/Socket#) -> TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder# {
159 TcpBuilderrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder# { socketrust-analyzer cargo net2 0.2.39 tcp/TcpBuilder#socket.: RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]new().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(sock)) }
160 }
161 }