Skip to content

Implement STL ourselves#259

Merged
axd1x8a merged 32 commits intomainfrom
feat/impl-stl-ourselves
Mar 15, 2026
Merged

Implement STL ourselves#259
axd1x8a merged 32 commits intomainfrom
feat/impl-stl-ourselves

Conversation

@axd1x8a
Copy link
Collaborator

@axd1x8a axd1x8a commented Mar 5, 2026

No description provided.

@axd1x8a axd1x8a force-pushed the feat/impl-stl-ourselves branch 2 times, most recently from e9979a6 to f535d4a Compare March 7, 2026 09:52
@axd1x8a axd1x8a marked this pull request as ready for review March 7, 2026 09:53
@axd1x8a axd1x8a requested review from Dasaav-dsv, nex3 and vswarte March 7, 2026 09:53
@axd1x8a axd1x8a mentioned this pull request Mar 7, 2026
@axd1x8a axd1x8a force-pushed the feat/impl-stl-ourselves branch from 8adce09 to a1a9a1f Compare March 7, 2026 14:19
Copy link
Collaborator

@nex3 nex3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I think it's probably worth adding unit tests for these, since they're very easy to isolate and somewhat algorithmically-intensive (particularly the RB tree).

  • I think all the collections should have Drop implementations even if we aren't constructing them ourselves yet, just so we don't shoot ourselves in the foot later by adding constructors. It's also possible that we'll end up owning some of them even without constructors (for example, if there's a collection of collections).

  • I looked at all the red-black logic, but I can't say I verified it in great depth.

  • Personally I would find it clearer to put this directly within crates but I don't feel that strongly about it.

axd1x8a and others added 3 commits March 7, 2026 17:24
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to publish fromsoftware-shared-stl instead

@axd1x8a axd1x8a force-pushed the feat/impl-stl-ourselves branch 3 times, most recently from cebdf96 to 44a19b1 Compare March 10, 2026 02:26
@axd1x8a axd1x8a requested review from nex3 and vswarte March 10, 2026 05:06
///
/// `size` must be non-zero. `align` must be a power of two.
/// The returned pointer is valid for `size` bytes and aligned to `align`
unsafe fn allocate_raw(&mut self, size: usize, align: usize) -> NonNull<c_void>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be unsafe? As long as the implementations do something sensible when the inputs are invalid (including erroring out) it should be safe.

Also, I don't think returning NonNull is correct here. It's possible for an allocator to return a null pointer if the allocation fails (and in fact we see checks for this all over the decomp).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think it's weird to both have a distinction between allocate_raw and allocate (which suggests that allocate_raw is a much thinner abstraction over the native C API), while also having allocate_raw return NonNull when the C APIs are going to universally return null pointers to indicate allocation failures. I think it's better to make this

Suggested change
unsafe fn allocate_raw(&mut self, size: usize, align: usize) -> NonNull<c_void>;
unsafe fn allocate_raw(&mut self, size: usize, align: usize) -> *mut c_void;

and handle the case where it returns null in allocate().

use std::{cmp::Ordering, mem::MaybeUninit, ptr::NonNull};

/// Comparator trait for use in MSVC `std::tree` [`RbTree`]
pub trait TreeComparator<V> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think that writing ___.gte(___) is clearer than !____.lt(____) and so on...

Co-authored-by: Natalie Weizenbaum <nex342@gmail.com>
@axd1x8a axd1x8a requested a review from nex3 March 11, 2026 03:37
///
/// `size` must be non-zero. `align` must be a power of two.
/// The returned pointer is valid for `size` bytes and aligned to `align`
unsafe fn allocate_raw(&mut self, size: usize, align: usize) -> NonNull<c_void>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think it's weird to both have a distinction between allocate_raw and allocate (which suggests that allocate_raw is a much thinner abstraction over the native C API), while also having allocate_raw return NonNull when the C APIs are going to universally return null pointers to indicate allocation failures. I think it's better to make this

Suggested change
unsafe fn allocate_raw(&mut self, size: usize, align: usize) -> NonNull<c_void>;
unsafe fn allocate_raw(&mut self, size: usize, align: usize) -> *mut c_void;

and handle the case where it returns null in allocate().

axd1x8a and others added 2 commits March 15, 2026 07:53
Co-authored-by: Natalie Weizenbaum <nex342@gmail.com>
@axd1x8a axd1x8a merged commit f78bfff into main Mar 15, 2026
1 check passed
@axd1x8a axd1x8a deleted the feat/impl-stl-ourselves branch March 15, 2026 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants