zajic
zajic

Reputation: 155

Rust: not exposing a shared library

I'm looking for best practice defining a project structure in Rust. Let's say I have a project that consists of a client and a server component, and they share some functionality I'd like to move to a separate common library. So all in all, like this:

my_awesome_project
 - common [library]
 - client [binary] - uses common
 - server [binary] - uses common

Obviously, all methods in the common crate used in either client or server must be public. However, I would like to avoid anyone who has the client binary (not the code) being able to call methods from common. Is that possible somehow?

I come from C# background, where common would be a DLL exposing public methods, easily callable. I've read that Rust uses static linking by default, but in my understanding, that does not provide what I am looking for.

And yes, I could double the common code in a private module for both server and client, but that's not optimal either.

Upvotes: 1

Views: 165

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 72054

To the extent that you have any control over what someone does with your code, Rust's static linking will sufficiently obfuscate the library boundary that you need not worry about the methods being callable.

You will not deliver the library to the user. You will deliver a single binary, hopefully stripped of debug information, which contains the code of client and all the code from common it needs, bundled together.

Unlike C#, there is no separate DLL file, and no metadata that makes it easy to discover and use library functions.

Upvotes: 2

Related Questions