Can the feature derive macro be applied only on functions and structures?

Asked 2 months ago, Updated 2 months ago, 3 views

I asked the same question on teratail, but I have not received an answer, so I will ask you the same question!

All of the information below is an abstraction of the story about this repository.
https://github.com/s3pt3mb3r/toy-arms
The repository contains folders named internal and external and feature.
I want to prevent external library users from using the public function written in the files in each folder unless the corresponding feature is on!

I wanted to separate the files under src of my lib project by function category.
If there were folders A and B, ideally if you specified features=["A"] on the user side of the library,

use LIBRARY_NAME::A::FUNCTION_NAME;
// Use LIBRARY_NAME::B::FUNCTION_NAME, etc. should not be allowed

I'd like to make it look like this.

Currently, the file structure is as follows:

src -- lib.rs
    |-- A
          |-- |-- mod.rs
          |-- |-- A.rs
    |-- B
          |-- |-- mod.rs
          |-- |-- B.rs

src/lib.rs is as follows:

#[cfg(feature="A")]
pub mod A;

# [cfg(feature="B")]
pub mod B;

A/mod.rs is as follows:

#[cfg(feature="A")]
pub mod A;
pubuse A::*;

B/mod.rs is similar to A as follows:

#[cfg(feature="B")]
pub mod B;
pubuse B::*;

However, if you specify features=["A"] from the current user side,

use LIBRARY_NAME::B::FUNCTION_NAME;

You can use for some reason.
I understand that you can annotate #[cfg(feature="A")] on functions and structures one after another, but is there any easier way?

rust

2022-09-30 14:12

1 Answers

It was resolved based on Pappy's
--no-default-features We have confirmed that it will become unusable by adding default-features=false to the flag or dependencies to disable the default feature.


2022-09-30 14:12

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.