A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://doc.rust-lang.org/nightly/src/core/net/socket_addr.rs.html below:

socket_addr.rs - source

1use super::display_buffer::DisplayBuffer;
2use crate::fmt::{self, Write};
3use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
4
5#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
32#[stable(feature = "rust1", since = "1.0.0")]
33pub enum SocketAddr {
34    #[stable(feature = "rust1", since = "1.0.0")]
36    V4(#[stable(feature = "rust1", since = "1.0.0")] SocketAddrV4),
37    #[stable(feature = "rust1", since = "1.0.0")]
39    V6(#[stable(feature = "rust1", since = "1.0.0")] SocketAddrV6),
40}
41
42#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
80#[stable(feature = "rust1", since = "1.0.0")]
81pub struct SocketAddrV4 {
82    ip: Ipv4Addr,
83    port: u16,
84}
85
86#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
146#[stable(feature = "rust1", since = "1.0.0")]
147pub struct SocketAddrV6 {
148    ip: Ipv6Addr,
149    port: u16,
150    flowinfo: u32,
151    scope_id: u32,
152}
153
154impl SocketAddr {
155    #[stable(feature = "ip_addr", since = "1.7.0")]
169    #[must_use]
170    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
171    #[inline]
172    pub const fn new(ip: IpAddr, port: u16) -> SocketAddr {
173        match ip {
174            IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
175            IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
176        }
177    }
178
179    #[must_use]
190    #[stable(feature = "ip_addr", since = "1.7.0")]
191    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
192    #[inline]
193    pub const fn ip(&self) -> IpAddr {
194        match *self {
195            SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
196            SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
197        }
198    }
199
200    #[inline]
212    #[stable(feature = "sockaddr_setters", since = "1.9.0")]
213    #[rustc_const_stable(feature = "const_sockaddr_setters", since = "1.87.0")]
214    pub const fn set_ip(&mut self, new_ip: IpAddr) {
215        match (self, new_ip) {
217            (&mut SocketAddr::V4(ref mut a), IpAddr::V4(new_ip)) => a.set_ip(new_ip),
218            (&mut SocketAddr::V6(ref mut a), IpAddr::V6(new_ip)) => a.set_ip(new_ip),
219            (self_, new_ip) => *self_ = Self::new(new_ip, self_.port()),
220        }
221    }
222
223    #[must_use]
234    #[stable(feature = "rust1", since = "1.0.0")]
235    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
236    #[inline]
237    pub const fn port(&self) -> u16 {
238        match *self {
239            SocketAddr::V4(ref a) => a.port(),
240            SocketAddr::V6(ref a) => a.port(),
241        }
242    }
243
244    #[inline]
256    #[stable(feature = "sockaddr_setters", since = "1.9.0")]
257    #[rustc_const_stable(feature = "const_sockaddr_setters", since = "1.87.0")]
258    pub const fn set_port(&mut self, new_port: u16) {
259        match *self {
260            SocketAddr::V4(ref mut a) => a.set_port(new_port),
261            SocketAddr::V6(ref mut a) => a.set_port(new_port),
262        }
263    }
264
265    #[must_use]
281    #[stable(feature = "sockaddr_checker", since = "1.16.0")]
282    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
283    #[inline]
284    pub const fn is_ipv4(&self) -> bool {
285        matches!(*self, SocketAddr::V4(_))
286    }
287
288    #[must_use]
304    #[stable(feature = "sockaddr_checker", since = "1.16.0")]
305    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
306    #[inline]
307    pub const fn is_ipv6(&self) -> bool {
308        matches!(*self, SocketAddr::V6(_))
309    }
310}
311
312impl SocketAddrV4 {
313    #[stable(feature = "rust1", since = "1.0.0")]
325    #[must_use]
326    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
327    #[inline]
328    pub const fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
329        SocketAddrV4 { ip, port }
330    }
331
332    #[must_use]
343    #[stable(feature = "rust1", since = "1.0.0")]
344    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
345    #[inline]
346    pub const fn ip(&self) -> &Ipv4Addr {
347        &self.ip
348    }
349
350    #[inline]
362    #[stable(feature = "sockaddr_setters", since = "1.9.0")]
363    #[rustc_const_stable(feature = "const_sockaddr_setters", since = "1.87.0")]
364    pub const fn set_ip(&mut self, new_ip: Ipv4Addr) {
365        self.ip = new_ip;
366    }
367
368    #[must_use]
379    #[stable(feature = "rust1", since = "1.0.0")]
380    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
381    #[inline]
382    pub const fn port(&self) -> u16 {
383        self.port
384    }
385
386    #[inline]
398    #[stable(feature = "sockaddr_setters", since = "1.9.0")]
399    #[rustc_const_stable(feature = "const_sockaddr_setters", since = "1.87.0")]
400    pub const fn set_port(&mut self, new_port: u16) {
401        self.port = new_port;
402    }
403}
404
405impl SocketAddrV6 {
406    #[stable(feature = "rust1", since = "1.0.0")]
423    #[must_use]
424    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
425    #[inline]
426    pub const fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 {
427        SocketAddrV6 { ip, port, flowinfo, scope_id }
428    }
429
430    #[must_use]
441    #[stable(feature = "rust1", since = "1.0.0")]
442    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
443    #[inline]
444    pub const fn ip(&self) -> &Ipv6Addr {
445        &self.ip
446    }
447
448    #[inline]
460    #[stable(feature = "sockaddr_setters", since = "1.9.0")]
461    #[rustc_const_stable(feature = "const_sockaddr_setters", since = "1.87.0")]
462    pub const fn set_ip(&mut self, new_ip: Ipv6Addr) {
463        self.ip = new_ip;
464    }
465
466    #[must_use]
477    #[stable(feature = "rust1", since = "1.0.0")]
478    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
479    #[inline]
480    pub const fn port(&self) -> u16 {
481        self.port
482    }
483
484    #[inline]
496    #[stable(feature = "sockaddr_setters", since = "1.9.0")]
497    #[rustc_const_stable(feature = "const_sockaddr_setters", since = "1.87.0")]
498    pub const fn set_port(&mut self, new_port: u16) {
499        self.port = new_port;
500    }
501
502    #[must_use]
523    #[stable(feature = "rust1", since = "1.0.0")]
524    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
525    #[inline]
526    pub const fn flowinfo(&self) -> u32 {
527        self.flowinfo
528    }
529
530    #[inline]
544    #[stable(feature = "sockaddr_setters", since = "1.9.0")]
545    #[rustc_const_stable(feature = "const_sockaddr_setters", since = "1.87.0")]
546    pub const fn set_flowinfo(&mut self, new_flowinfo: u32) {
547        self.flowinfo = new_flowinfo;
548    }
549
550    #[must_use]
566    #[stable(feature = "rust1", since = "1.0.0")]
567    #[rustc_const_stable(feature = "const_socketaddr", since = "1.69.0")]
568    #[inline]
569    pub const fn scope_id(&self) -> u32 {
570        self.scope_id
571    }
572
573    #[inline]
587    #[stable(feature = "sockaddr_setters", since = "1.9.0")]
588    #[rustc_const_stable(feature = "const_sockaddr_setters", since = "1.87.0")]
589    pub const fn set_scope_id(&mut self, new_scope_id: u32) {
590        self.scope_id = new_scope_id;
591    }
592}
593
594#[stable(feature = "ip_from_ip", since = "1.16.0")]
595#[rustc_const_unstable(feature = "const_try", issue = "74935")]
596impl const From<SocketAddrV4> for SocketAddr {
597    #[inline]
599    fn from(sock4: SocketAddrV4) -> SocketAddr {
600        SocketAddr::V4(sock4)
601    }
602}
603
604#[stable(feature = "ip_from_ip", since = "1.16.0")]
605#[rustc_const_unstable(feature = "const_try", issue = "74935")]
606impl const From<SocketAddrV6> for SocketAddr {
607    #[inline]
609    fn from(sock6: SocketAddrV6) -> SocketAddr {
610        SocketAddr::V6(sock6)
611    }
612}
613
614#[stable(feature = "addr_from_into_ip", since = "1.17.0")]
615#[rustc_const_unstable(feature = "const_try", issue = "74935")]
616impl<I: [const] Into<IpAddr>> const From<(I, u16)> for SocketAddr {
617    fn from(pieces: (I, u16)) -> SocketAddr {
624        SocketAddr::new(pieces.0.into(), pieces.1)
625    }
626}
627
628#[stable(feature = "rust1", since = "1.0.0")]
629impl fmt::Display for SocketAddr {
630    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
631        match *self {
632            SocketAddr::V4(ref a) => a.fmt(f),
633            SocketAddr::V6(ref a) => a.fmt(f),
634        }
635    }
636}
637
638#[stable(feature = "rust1", since = "1.0.0")]
639impl fmt::Debug for SocketAddr {
640    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
641        fmt::Display::fmt(self, fmt)
642    }
643}
644
645#[stable(feature = "rust1", since = "1.0.0")]
646impl fmt::Display for SocketAddrV4 {
647    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
648        if f.precision().is_none() && f.width().is_none() {
651            write!(f, "{}:{}", self.ip(), self.port())
652        } else {
653            const LONGEST_IPV4_SOCKET_ADDR: &str = "255.255.255.255:65535";
654
655            let mut buf = DisplayBuffer::<{ LONGEST_IPV4_SOCKET_ADDR.len() }>::new();
656            write!(buf, "{}:{}", self.ip(), self.port()).unwrap();
658
659            f.pad(buf.as_str())
660        }
661    }
662}
663
664#[stable(feature = "rust1", since = "1.0.0")]
665impl fmt::Debug for SocketAddrV4 {
666    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
667        fmt::Display::fmt(self, fmt)
668    }
669}
670
671#[stable(feature = "rust1", since = "1.0.0")]
672impl fmt::Display for SocketAddrV6 {
673    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
674        if f.precision().is_none() && f.width().is_none() {
677            match self.scope_id() {
678                0 => write!(f, "[{}]:{}", self.ip(), self.port()),
679                scope_id => write!(f, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
680            }
681        } else {
682            const LONGEST_IPV6_SOCKET_ADDR: &str =
683                "[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%4294967295]:65535";
684
685            let mut buf = DisplayBuffer::<{ LONGEST_IPV6_SOCKET_ADDR.len() }>::new();
686            match self.scope_id() {
687                0 => write!(buf, "[{}]:{}", self.ip(), self.port()),
688                scope_id => write!(buf, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
689            }
690            .unwrap();
692
693            f.pad(buf.as_str())
694        }
695    }
696}
697
698#[stable(feature = "rust1", since = "1.0.0")]
699impl fmt::Debug for SocketAddrV6 {
700    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
701        fmt::Display::fmt(self, fmt)
702    }
703}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4