Make RawSmallVec (and its fields) public only under feature = "internals". (v2) - #348
Conversation
|
I just saw #245 which may make the "Alternately" the desired route. |
|
I do get the motivation behind it: other what about having a feature flag that makes it public?? e.g. "rawsmallvec" (or even "internals" if we are going to re-export other types as well) so by default that feature would be disabled, but could be turned on by other crates to access it it wouldn't be a great idea to export it in the public api by default because it's not a type that's meant to be used as such by most downstream crates |
|
also, sorry for the long delay |
Currently, only the `RawSmallVec` type (and its fields) are exposed under this feature.
afdec01 to
1861ee4
Compare
|
We could add an cargo feature to make I've pushed 1861ee4 (after a rebase) which makes |
RawSmallVec (and its fields) public only under feature = "internals".
RawSmallVec (and its fields) public only under feature = "internals".RawSmallVec (and its fields) public only under feature = "internals". (v2)
|
(Also changes the doc comment to not claim that there's a niche, since unions (currently) never have niches) |
good, thanks
I literally spent a few minutes this morning looking at those two structs, there's no way I found that scales with size and has that performance so we can be pretty sure that the union won't change and we wouldn't be able to switch to something like |
| pub union RawSmallVec<T, const N: usize> { | ||
| inline: ManuallyDrop<MaybeUninit<[T; N]>>, | ||
| heap: (NonNull<T>, usize), | ||
| mod raw { |
There was a problem hiding this comment.
remove this unnecessary module
the way is to leave it as it was, and then add a re-export
so:
#[cfg(feature = "internals")]
pub use RawSmallVec; // self::RawSmallVecThere was a problem hiding this comment.
I tried this, and it doesn't work, for two reasons:
- you can't
pub usean item that isn'tpub - you can't have two items in the same module with the same name, and a
usecounts as a separate item even if the name it conflicts with is the thing it is re-exporting.
Details
```rs
struct Foo;
pub use Foo;error[E0255]: the name `Foo` is defined multiple times
--> src/lib.rs:3:9
|
1 | struct Foo;
| ----------- previous definition of the type `Foo` here
2 |
3 | pub use Foo;
| ^^^ `Foo` reimported here
|
= note: `Foo` must be defined only once in the type namespace of this module
help: you can use `as` to change the binding name of the import
|
3 | pub use Foo as OtherFoo;
| +++++++++++
error[E0364]: `Foo` is only public within the crate, and cannot be re-exported outside
--> src/lib.rs:3:9
|
3 | pub use Foo;
| ^^^
|
note: consider marking `Foo` as `pub` in the imported module
--> src/lib.rs:3:9
|
3 | pub use Foo;
| ^^^There was a problem hiding this comment.
oh yeah this is bad, you're right
so when it's the same file it'd do this stuff
actually do this instead
move the full pub RawSmallVec implementation into a separate module like rawsmallvec.rs and which is simply a mod rawsmallvec:: and then the lib.rs re-exports it (properly)
this is part of #427, that we haven't implemented yet
There was a problem hiding this comment.
we could instead have a macro emit the declaration, like
macro_rules! declare_rsv {
($vis:vis) => {
$vis union Whatever { /* ... */ }
};
}
#[cfg(feature = "internals")]
declare_rsv!(pub);
#[cfg(not(feature = "internals"))]
declare_rsv!(pub(crate));There was a problem hiding this comment.
no need to do so
with
mod rawsmallvec;
#[cfg(feature = "internals")]
pub use rawsmallvec::RawSmallVec;
#[cfg(not(feature = "internals"))] // if this is needed, now it is at least
use rawsmallvec::RawSmallVec;rust book's rule: don't make a macro out of which doesn't need to be a macro
and we do have to modularize lib.rs because having a 3k file encompassing the whole universe is not very manageable
There was a problem hiding this comment.
Okay, latest push moves mod raw to a file and renames it rawsmallvec (and rearranges the imports a bit).
👍
The length of the |
it doesn't sound bad but it'd be a nightmare to implement it and would make things worse probably I'm not completely sure, I haven't dug very deep into it I annotated it anyway. |
| @@ -0,0 +1,13 @@ | |||
| use core::mem::{ManuallyDrop, MaybeUninit}; | |||
| use core::ptr::NonNull; | |||
There was a problem hiding this comment.
Not sure splitting to its own file is really beneficial but...
It doesn't have any public API other than auto traits and blanket impls, and no other public APIs take or return it, so it probably doesn't need to be public.
(Alternately, it's fields could be maked
pubso external users could use it for their own purposes, but it would probably be better for them to just define their own version.)