Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking Issue for ip_as_octets #137259

Open
1 of 4 tasks
mammothbane opened this issue Feb 19, 2025 · 0 comments
Open
1 of 4 tasks

Tracking Issue for ip_as_octets #137259

mammothbane opened this issue Feb 19, 2025 · 0 comments
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@mammothbane
Copy link
Contributor

Feature gate: #![feature(ip_as_octets)]

This is a tracking issue for enabling reference access to IpAddr, Ipv4Addr, and Ipv6Addr contents. Previously, only an array copy was exposed via .octets().

Public API

impl core::net::IpAddr {
    pub const fn as_octets(&self) -> &[u8];
}

impl core::net::Ipv4Addr {
    pub const fn as_octets(&self) -> &[u8; 4];
}

impl core::net::Ipv6Addr {
    pub const fn as_octets(&self) -> &[u8; 16];
}

Steps / History

Unresolved Questions

  • @tgross35 offered a potential alternative in the ACP: that we instead provide #[repr(transparent)] for these types and document layout stability, leaving transmutation to a byte slice to third-party libraries.
@mammothbane mammothbane added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Feb 19, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 21, 2025
libcore/net: `IpAddr::as_octets()`

[ACP](rust-lang/libs-team#535)
[Tracking issue](rust-lang#137259)

Adds `const` `core::net::IpAddr{,v4,v6}::as_octets()` methods to provide reference access to IP address contents.

The concrete usecase for me is allowing the `IpAddr` to provide an extended lifetime in contexts that want a `&[u8]`:

```rust
trait AddrSlice {
    fn addr_slice(&self) -> &[u8];
}

impl AddrSlice for IpAddrV4 {
    fn addr_slice(&self) -> &[u8] {
        // self.octets() doesn't help us here, because we can't return a reference to the owned array.
        // Instead we want the IpAddrV4 to continue owning the memory:
        self.as_octets()
    }
}
```

(Notably, in this case we can't parameterize `AddrSlice` by a `const N: usize` (such that `fn addr_slice(&self) -> [u8; N]`) and maintain object-safety.)
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 21, 2025
Rollup merge of rust-lang#136609 - mammothbane:master, r=scottmcm

libcore/net: `IpAddr::as_octets()`

[ACP](rust-lang/libs-team#535)
[Tracking issue](rust-lang#137259)

Adds `const` `core::net::IpAddr{,v4,v6}::as_octets()` methods to provide reference access to IP address contents.

The concrete usecase for me is allowing the `IpAddr` to provide an extended lifetime in contexts that want a `&[u8]`:

```rust
trait AddrSlice {
    fn addr_slice(&self) -> &[u8];
}

impl AddrSlice for IpAddrV4 {
    fn addr_slice(&self) -> &[u8] {
        // self.octets() doesn't help us here, because we can't return a reference to the owned array.
        // Instead we want the IpAddrV4 to continue owning the memory:
        self.as_octets()
    }
}
```

(Notably, in this case we can't parameterize `AddrSlice` by a `const N: usize` (such that `fn addr_slice(&self) -> [u8; N]`) and maintain object-safety.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant