Best practice for making alloc optional

What example should I look at to learn the best practice patterns for making the parts of an existing crate in such a way that they turn on if any dependent wants them but dependents that don't need them can declare themselves as not using an allocator?

Concretely, I'd like to make chardetng say that it doesn't need std or alloc and make it depend on encoding_rs in a way that turns off std and alloc, but if another dependent of encoding_rs wants the alloc-using features, I'd like there to be one copy of encoding_rs with alloc enabled. And encoding_rs should default to having the alloc-using bits for compatibility, since it now has those unconditionally.

Features are unified and additive, so rather than "a way that turns off std and alloc", you should use a feature that turns those on, and you can add that to the default feature set.

If encoding_rs has an "alloc" feature, then in chardetng you would also have [feature] alloc = ["encoding_rs/alloc"]. There's not (currently) a way to detect that someone else has turned on "encoding_rs/alloc" to have that affect chardetng, but you'll still get only one build of encoding_rs.


Thanks. I'll add a default feature alloc for encoding_rs and will turn default features for encoding_rs off when using it from chardetng.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.