#thread #cell #accessed #single #owner #different #panicked

single_thread_cell

Create a cell that can only be accessed by a single thread

3 releases (breaking)

0.3.0 Jan 24, 2025
0.2.0 Jan 23, 2025
0.1.0 Jan 22, 2025

#5 in #owner

Download history 312/week @ 2025-01-20 25/week @ 2025-01-27 17/week @ 2025-02-03

354 downloads per month

Apache-2.0

11KB
213 lines

Introduction

This is a helper library to mark the cell as only being accessed by the owner thread.

If you access the cell from a different thread, the thread will be panicked.

Still in development, the API may change in the future.

Quick Start

use single_thread_cell::{SingleThreadCell, SingleThreadRefCell};

let cell = SingleThreadCell::new(0);
assert_eq!(cell.get(), 0);
cell.set(1);
assert_eq!(cell.get(), 1);

let ref_cell = SingleThreadRefCell::new(0);
assert_eq!(*ref_cell.borrow(), 0);
*ref_cell.borrow_mut() += 1;
assert_eq!(*ref_cell.borrow(), 1);

lib.rs:

This is a helper library to mark the cell as only being accessed by the owner thread.

If you access the cell from a different thread, the thread will be panicked.

The only exception is drop. The cell does not implement Send if T is not Send. So that the cell cannot be sent to another thread to drop. It is obvious that if T is Send, it is safe to drop in the other thread.

No runtime deps