rust-lang / rust-lang/hashbrown

Conversion of raw pointers into buckets

Open
#435 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3k
Forks
359
Avg merge
11h 57m
Merged PRs (30d)
2

Description

Currently, it is possible to convert Buckets into raw pointers through Bucket::as_ptr, but I was unable to find a reverse. Would it be possible to offer a [unsafe] fn Bucket::from_ptr(ptr: *mut T) -> Bucket<T>?

As a use case, I am using raw pointers into a raw table and would like to efficiently store optional pointers as nullable. A function like this would allow me to model it as follows.

struct OptionalBucket<T> {
    ptr: *mut T
}

impl<T> OptionalBucket<T> {
    fn some(bucket: Bucket<T>) -> OptionalBucket<T> {
        OptionalBucket { ptr: bucket.as_ptr() }
    }
    
    fn none() -> OptionalBucket<T> {
        OptionalBucket { ptr: std::ptr::null_mut() }
    }
    
    fn as_option(self) -> Option<Bucket<T>> {
        if self.ptr.is_null() { None } else { Some(Bucket::from_ptr(self.ptr)) }
    }
}

The best I found is the ugly and implementation-dependent std::mem::transmute(ptr.add(1)).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing Bucket::as_ptr API and the raw-table usage described in the issue. Determine whether a supported reverse conversion can preserve Bucket's safety and representation invariants, including the nullable-pointer use case. Done means the API decision and its safety requirements are clear, with coverage for the supported conversion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.