How to throw a "feature" inside the library depending on the "feature" of the main program

I am poorly acquainted with Rust. Is it possible to ask for help among experts. Thanks in advance for the time spent for decision of my problem.

I have prepared an example to demonstrate problem. What is expected to get:
After the command
cargo build --features "qsock"
inside the somelib, the code for
#[CFG (feature = "qsock")
should be executed and not be executed for
#[CFG (feature = "vsock").

And vice versa. After the command
cargo build --features "vsock"
inside the somelib, the code for
#[CFG (feature = "vsock")]
should be executed and not be executed for
#[CFG (feature = "qsock")].

Topology of execution:

hello_cargo -----> some_lib -----> qqsock           

hello_cargo->src->main.rs

use some_lib::SomeLibStruct;

fn main() {
    println!("Hello, world!");

    #[cfg(feature = "qsock")] {
        println!("feature = qsoc from hello_cargo");
        SomeLibStruct::th_bind(7);
    }
    #[cfg(feature = "vsock")] {
        println!("feature = vsoc from hello_cargo");
        SomeLibStruct::th_bind(17);
    }
}

hello_cargo->cargo.toml

[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

[dependencies]
some_lib = {path = "../somelib", features = ["qsock", "vsock"]}
#some_lib = {path = "../somelib"}

[features]
qsock = []
vsock = []

some_lib->src->lib.rs

#[cfg(feature = "qsock")]
use qqsock::QSockListener;

pub struct SomeLibStruct {
    pub some_var: u32,
}

impl SomeLibStruct {
    pub fn th_bind(input: u32) {
        println!("from somelib input = {}", input);

        #[cfg(feature = "qsock")] {
            println!("feature = qsoc from somelib");
            QSockListener::bind(25);
        }

        #[cfg(feature = "vsock")]
        println!("feature = vsoc from somelib");
    }
}

somelib->cargo.toml

[package]
name = "some_lib"
edition = "2021"
version = "0.20.0"

[dependencies]
qqsock = {path = "../qqsock", optional = true}

[features]
qsock = ["dep:qqsock"]
vsock = []

qqsock->src->lib.rs

#[derive(Debug)]
pub struct QSockListener {
    pub some_var: u32,
}

impl QSockListener {
    pub fn bind(input: u32){
        println!("qqsock lib bind. input = {}", input);
    }
}

What turns out:
Output of cargo build --features "vsock":
./hello_cargo

Hello, world!
feature = vsoc from hello_cargo
from somelib input = 17
feature = qsoc from somelib
qqsock lib bind. input= 25
feature = vsoc from somelib

Output of cargo build --features "qsock":
./hello_cargo

Hello, world!
feature = qsoc from hello_cargo
from somelib input = 7
feature = qsoc from somelib
qqsock lib bind. input = 25
feature = vsoc from somelib

Expected:
Output of cargo build --features "vsock":
./hello_cargo

Hello, world!
feature = vsoc from hello_cargo
from somelib input = 17
feature = vsoc from somelib

Output of cargo build --features "qsock":
./hello_cargo

Hello, world!
feature = qsoc from hello_cargo
from somelib input = 7
feature = qsoc from somelib
qqsock lib bind. input = 25

You can enable features of your dependencies through your own features like this:

hello_cargo/Cargo.toml:

[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

[dependencies]
some_lib = {path = "../somelib"}

[features]
qsock = ["some_lib/qsock"]
vsock = ["some_lib/vsock"]
2 Likes

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.