-
Notifications
You must be signed in to change notification settings - Fork 5
Use weak symbols to allow using default trait methods #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for using weak symbols to enable default trait method implementations to be called via call_interface!. This feature is gated behind a weak_default feature flag and requires nightly Rust with the linkage feature.
Changes:
- Added
weak_defaultfeature flag to Cargo.toml with documentation - Modified
def_interfacemacro to generate weak symbol functions for methods with default implementations when the feature is enabled - Added validation to reject methods with
selfparameters when using weak_default - Added comprehensive test file for the new feature
- Removed default implementation from test_crate_interface.rs to maintain test validity
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| Cargo.toml | Adds weak_default feature flag with inline documentation |
| src/lib.rs | Implements weak symbol generation for default trait methods with self-parameter validation and feature-gated code |
| tests/test_weak_default.rs | New test file demonstrating weak symbol functionality with partial trait implementations |
| tests/test_crate_interface.rs | Removes default implementation from foo() method to maintain test validity without weak_default feature |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
For your reference, we implemented the "real" trait in https://github.com/Starry-OS/extern-trait by generating code using |
|
Another reference is that Rust's official implementation of external functions has already been implemented in nightly, with the feature name |
EII is still in its early stage, and I don't think we can migrate to eii in one or two years (RFC3635, for eii functions, and 3645, for eii traits, are not formally merged yet), so it's still worthy to make crate_interface better now. |
- Introduced a new feature `weak_default` in Cargo.toml to generate weak symbol functions for trait methods with default implementations. - Updated `def_interface` function in lib.rs to handle weak symbol generation, ensuring methods with `self` parameters are not supported. - Modified test cases to reflect the updated trait method signatures.
13dcbe5 to
4e832a4
Compare
|
Something to discuss: what's the expected behavior in the following case? #[def_interface]
trait If {
fn x() -> i32 { 42 }
fn print_x() { println!("{}", Self::x()) }
}
struct Impl;
#[impl_interface]
impl If for Impl {
fn x() -> i32 { 100 }
}
fn main() {
call_interface!(If::print_x);
}
Nevermind, let the user override it. |
…is not enabled, add multi-crate tests
|
Known issue: calling associated methods in the default implementation of a method in the same trait results in compiler error. Expected to be fixed soon. |
Already fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 26 out of 27 changed files in this pull request and generated 9 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Currently, the default implementations of methods in crate_interface traits cause some inconsistencies. They:
call_interface!.It's frustrating in at least two ways: ignoring methods with default implementations in
#[impl_interface]causes no compiler error, while calling them withcall_interface!causes link-time errors (instead of calling the default implementations). It violates the principle of least surprise. Take the following snippet as an example:Also, providing default implementations for some crate_interface methods is extremely useful, especially when designing and implementing BSPs.
It's possible to use weak symbols as a solution: the default implementations are wrapped and exported in functions with same signature as the expected implementor and marked as
#[linkage = "weak"], allowing the use of them withcall_interface!, while implementations in#[impl_interface]blocks can safely override them. For backward compatibility, this feature is gated by the "weak_default" feature.Possible drawbacks:
linkage, until Tracking issue for thelinkagefeature rust-lang/rust#29603 is stabilized.Possible improvements and fixes that can be shipped with this PR:
self-like receivers in all crate_interface methods.