Sfoglia il codice sorgente

Allow get offset of slots field of `StaticThingBuf` (#1)

LoGin 1 anno fa
parent
commit
2dded730c3
5 ha cambiato i file con 21 aggiunte e 1 eliminazioni
  1. 1 0
      Cargo.toml
  2. 1 0
      src/lib.rs
  3. 1 0
      src/loom.rs
  4. 6 0
      src/static_thingbuf.rs
  5. 12 1
      tests/static_storage.rs

+ 1 - 0
Cargo.toml

@@ -29,6 +29,7 @@ static = []
 [dependencies]
 pin-project = "1"
 parking_lot = { version = "0.12", optional = true, default-features = false }
+memoffset = "0.9.0"
 
 [dev-dependencies]
 tokio = { version = "1.14.0", features = ["rt", "rt-multi-thread", "macros", "sync"] }

+ 1 - 0
src/lib.rs

@@ -1,3 +1,4 @@
+#![feature(const_refs_to_cell)]
 #![cfg_attr(not(feature = "std"), no_std)]
 #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
 #![doc = include_str!("../README.md")]

+ 1 - 0
src/loom.rs

@@ -204,6 +204,7 @@ mod inner {
 mod inner {
     #![allow(dead_code)]
     pub(crate) mod sync {
+        #[allow(unused)]
         pub use core::sync::*;
 
         #[cfg(feature = "alloc")]

+ 6 - 0
src/static_thingbuf.rs

@@ -192,6 +192,7 @@ use core::fmt;
 /// [vyukov]: https://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue
 /// [object pool]: https://en.wikipedia.org/wiki/Object_pool_pattern
 #[cfg_attr(docsrs, doc(cfg(feature = "static")))]
+#[repr(C)]
 pub struct StaticThingBuf<T, const CAP: usize, R = recycling::DefaultRecycle> {
     core: Core,
     recycle: R,
@@ -302,6 +303,11 @@ impl<T, const CAP: usize, R> StaticThingBuf<T, CAP, R> {
         self.core.len()
     }
 
+    /// Returns the offset of the `slots` field in the `StaticThingBuf` struct.
+    pub const fn offset_of_slots(&self) -> usize {
+        memoffset::offset_of!(Self, slots)
+    }
+
     /// Returns `true` if there are currently no elements in this `StaticThingBuf`.
     ///
     /// # Examples

+ 12 - 1
tests/static_storage.rs

@@ -1,10 +1,15 @@
+#![feature(offset_of)]
 #![cfg(feature = "static")]
 use std::{
     fmt::Write,
+    mem::size_of,
     sync::atomic::{AtomicBool, Ordering},
     thread,
 };
-use thingbuf::{recycling, StaticThingBuf};
+use thingbuf::{
+    recycling::{self, DefaultRecycle},
+    StaticThingBuf,
+};
 
 #[test]
 fn static_storage_thingbuf() {
@@ -47,6 +52,12 @@ fn static_storage_thingbuf() {
     }
 }
 
+#[test]
+fn static_storage_thingbuf_offset_of_slot_field() {
+    static BUF: StaticThingBuf<i32, 16> = StaticThingBuf::new();
+    assert_eq!(BUF.offset_of_slots(), 384 + size_of::<DefaultRecycle>());
+}
+
 #[test]
 fn static_storage_stringbuf() {
     use recycling::WithCapacity;