Skip to content

feat: hide allocator functions when unusable - #754

Open
cooronx wants to merge 1 commit into
servo:v2from
cooronx:feat/hide-unusable-allocator
Open

cooronx wants to merge 1 commit into
servo:v2from
cooronx:feat/hide-unusable-allocator

Conversation

@cooronx

@cooronx cooronx commented Oct 8, 2026

Copy link
Copy Markdown

issue #725

when no allocator feature is enabled, hide them from public API

@alejandro-vaz alejandro-vaz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

it works but I don't think this is the way

I was thinking more of having conditional visibility based on attributes

something similar to what the add-syntax crate does but just about visibility

@cooronx

cooronx commented Oct 9, 2026

Copy link
Copy Markdown
Author

I found a crate called visibility, seems to meet our requirements
it works like this

#[cfg_attr(
    any(feature = "feature_a", feature = "feature_b"),
    visibility::make(pub)
)]
fn test_fn() {
    println!("hello");
}

https://crates.io/crates/visibility

https://github.com/danielhenrymantilla/visibility.rs

@alejandro-vaz

Copy link
Copy Markdown
Collaborator

yeah but we can't add the crate as dependency

this is smallvec, each dependency is a potential risk, and depending on small crates with an unknown dependency graph is extremely risky for the ecosystem

whatever we do, it has to be self-hosted

@cooronx

cooronx commented Oct 9, 2026

Copy link
Copy Markdown
Author

i think we can write our own proc-marco, do the same thing as visibility does. not sure if this is the way you mean it.

@alejandro-vaz

Copy link
Copy Markdown
Collaborator

yeah I think that's the way

@cooronx
cooronx force-pushed the feat/hide-unusable-allocator branch from 0909ccf to 70aec69 Compare October 9, 2026 11:42
@alejandro-vaz

Copy link
Copy Markdown
Collaborator

wait wait wait let's think through it again

I don't like the idea we have another crate to publish

@alejandro-vaz

Copy link
Copy Markdown
Collaborator

don't we have any way to do the same thing without having a completely new crate to publish??

@cooronx

cooronx commented Oct 9, 2026

Copy link
Copy Markdown
Author

hi, I've updated the PR to implement the proc-macro we discussed — it works, and there are no external dependencies. but proc macros have to live in their own crate, That feels like a lot of overhead for something small.

@cooronx

cooronx commented Oct 9, 2026

Copy link
Copy Markdown
Author

wait wait wait let's think through it again

I don't like the idea we have another crate to publish

I also find it quite odd, but I'm not sure how else to implement it.

@cooronx

cooronx commented Oct 9, 2026

Copy link
Copy Markdown
Author

don't we have any way to do the same thing without having a completely new crate to publish??

maybe just back to the original approach instead, copy the api twice?

@alejandro-vaz

Copy link
Copy Markdown
Collaborator

I don't like copying things twice, it's just a bad practice

can we hack ourselves into it with a simple macro-rules macro that injects a pub keyword if an attribute is true??

like

macro_rules! public {
    ($attribute:meta) => {{
        #[cfg($attribute)]
        pub
    }}
}

or something weird like that, idk

@alejandro-vaz alejandro-vaz linked an issue Oct 9, 2026 that may be closed by this pull request
@cooronx
cooronx force-pushed the feat/hide-unusable-allocator branch from 70aec69 to 5035f99 Compare October 9, 2026 14:55
Comment thread smallvec-macros/src/lib.rs Outdated
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can be removed

Comment thread smallvec-macros/src/lib.rs Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we can move this file to src/macros.rs and convert that file to a proc-macro crate itself and then take the normal smallvec crate and re-export all those macros

Comment thread smallvec-macros/Cargo.toml Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

now that I think about it, this means we have to publish a new crate

I don't like that idea

Comment thread src/macros.rs

/// Makes the wrapped function part of the public API when the `#[cfg]`
/// predicate is true, and keeps it private otherwise.
macro_rules! pub_if {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we should rename it to something simpler like public

Comment thread src/macros.rs

#[cfg(not($($cond)*))]
$(#[$attr])*
pub(crate) const fn $($rest)*

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we can remove the pub(crate) and just leave it private as-is

Comment thread src/macros.rs

#[cfg(not($($cond)*))]
$(#[$attr])*
pub(crate) fn $($rest)*

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same as above

Comment thread src/macros.rs
Comment on lines +23 to +24
/// Makes the wrapped function part of the public API when the `#[cfg]`
/// predicate is true, and keeps it private otherwise.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this is not exported so we can simply remove the doc comment

Comment thread src/lib.rs
pub fn with_capacity_in(capacity: usize, allocator: Heap) -> Self {
Self::try_with_capacity_in(capacity, allocator).unwrap_or_else(SmallVecError::handle)
pub_if! {
#[cfg(any(feature = "allocator-api", feature = "allocator-api2"))]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this is the same as #[cfg(feature = "allocator-api")]

Comment thread src/lib.rs
.try_grow_raw(LocatedLength::new(0, false), capacity, &this.allocator)
}?;
pub_if! {
#[cfg(any(feature = "allocator-api", feature = "allocator-api2"))]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same as below

Comment thread src/lib.rs
allocator
impl<Item, const INLINE: usize, Heap: Allocator> SmallVec<Item, INLINE, Heap> {
pub_if! {
#[cfg(any(feature = "allocator-api", feature = "allocator-api2"))]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same as below

@alejandro-vaz

Copy link
Copy Markdown
Collaborator

the only thing I don't like about this approach is that processing the token stream disables IDE help

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.

hide allocator related functions on unusable

2 participants