-
Notifications
You must be signed in to change notification settings - Fork 114
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
Refactor: Replace dyn Trait with Enums #1462
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.
Looks good, thanks! Before ack, please resolve the conflict with the latest version.
I've also realized, that we could get rid of #[async_trait]
added to every Storage
trait and its FileStorage
implementation.
Could you remove the #[async_trait]
from all the traits under server/streaming/storage.rs
and their corresponding implementations? This will be another step further into optimizing these things :)
After removing async_trait, I encountered the following warning: warning: use of `async fn` in public traits is discouraged as auto trait bounds cannot be specified
--> server/src/streaming/storage.rs:83:5
|
83 | async fn load(&self) -> Result<SystemInfo, IggyError>;
| ^^^^^
|
= note: you can suppress this lint if you plan to use the trait only in your own code, or do not care about auto traits like `Send` on the `Future`
= note: `#[warn(async_fn_in_trait)]` on by default
help: you can alternatively desugar to a normal `fn` that returns `impl Future` and add any desired bounds such as `Send`, but these cannot be relaxed without a breaking API change
|
83 - async fn load(&self) -> Result<SystemInfo, IggyError>;
83 + fn load(&self) -> impl std::future::Future<Output = Result<SystemInfo, IggyError>> + Send;
| To resolve this, I modified the trait to use impl Future, like so: pub trait PartitionStorage: Send {
fn load(
&self,
partition: &mut Partition,
state: PartitionState,
) -> impl Future<Output = Result<(), IggyError>> + Send;
} However, after discussing this with @spetz, he mentioned that he didn’t face any warnings after removing async_trait. It’s possible that updating Rust to the latest version (rustup update) or running cargo clean to ensure no outdated build artifacts are causing issues might resolve the problem. I’ll try this approach and see if it works without further adjustments |
Yes, I do confirm that it compiles just fine on my side. |
Regardless if it compiles or not, it is better practice to design those apis in a way that you propose with For example #[derive(Debug)]
pub struct SystemStorage {
pub info: Arc<SystemInfoStorageKind>,
pub stream: Arc<StreamStorageKind>,
pub topic: Arc<TopicStorageKind>,
pub partition: Arc<PartitionStorageKind>,
pub segment: Arc<SegmentStorageKind>,
pub persister: Arc<PersisterKind>,
} Could be replaced with #[derive(Debug)]
pub struct SystemStorage<I, ST, T, P, S, PS>
where I: SystemInfoStorage,
ST: StreamStorage,
T: TopicStorage,
P : PartitionStorage,
S: SegmentStorage
PS: Persister
{
pub info: Arc<I>,
pub stream: Arc<ST>,
pub topic: Arc<T>,
pub partition: Arc<P>,
pub segment: Arc<S>,
pub persister: Arc<PS>,
} We will merge this pr once you get rid of the |
Yes, I’ve already thought about this while considering the options. It just seems that in this case, we’d have to add generics everywhere they’re used, which might make the code look overly cumbersome
Yeah, ok :) |
Removed async_trait from the server dependencies. Left the fix with impl Future based on the comment:
|
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.
LGTM :D
Addresses part of ##667
This PR refactors the codebase to replace the use of dyn Trait with enums for Sender, Archiver, State, Storage, and ConfigProvider