netstack3_base/socket/
cookie.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use netstack3_sync::rc::ResourceToken;
6
7/// Socket cookie is a unique 64-bit value assigned to a socket.
8///
9/// Socket implementations set their cookie value based on the `ResourceId`.
10#[derive(Debug)]
11#[cfg_attr(any(test, feature = "testutils"), derive(PartialEq, Eq, PartialOrd, Ord))]
12pub struct SocketCookie {
13    token: ResourceToken<'static>,
14}
15
16impl SocketCookie {
17    /// Creates a new cookie from the socket's `ResourceToken`.
18    pub fn new(token: ResourceToken<'_>) -> Self {
19        // Extend the lifetime of the token since `SocketCookie` is allowed to
20        // outlive the strong resource reference.
21        let token = token.extend_lifetime();
22        Self { token }
23    }
24
25    /// Returns the cookie value.
26    pub fn export_value(self) -> u64 {
27        self.token.export_value()
28    }
29}