Is it possible to implement a library on top of another one?

Let's say I have a library with various modules in it and different constants defined.
Is it possible to somehow use that library as dependency and create another one (extended version) which contains redefined const values for functions in original lib?
Something like:

original lib.rs

const VER: &str = "original";

pub mod version {
    pub fn get_version() -> &str {
        super::VER
    }
}

extended lib.rs

use original_lib::*;

const VER: &str = "extended";
. . . // ???

so that I could do

#[cfg(feature = "orig")]
extern crate lib_orig as lib;

#[cfg(feature = "ext")]
extern crate lib_ext as lib;

fn main() {
    let ver = lib::version::get_version(); // ver either "original" or "extended" based on selected feature
}

You can certainly layer crates -- e.g. rayon re-exports items from rayon-core. But no, you can't change any of the items that the lower level is using.

1 Like

You can use other libraries as your dependencies. You can enable Cargo features for them, but you can't influence them from your code. That's because they are compiled before your crate is compiled, and at that point it's too late to change anything. They're also shared between all crates that use them, so there can't be a situation where two crates tell them to do conflicting things.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.