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)]
11pub struct SocketCookie {
12    token: ResourceToken<'static>,
13}
14
15impl SocketCookie {
16    /// Creates a new cookie from the socket's `ResourceToken`.
17    pub fn new(token: ResourceToken<'_>) -> Self {
18        // Extend the lifetime of the token since `SocketCookie` is allowed to
19        // outlive the strong resource reference.
20        let token = token.extend_lifetime();
21        Self { token }
22    }
23
24    /// Returns the cookie value.
25    pub fn export_value(self) -> u64 {
26        self.token.export_value()
27    }
28}