Skip to content
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

Add global state management #187

Closed
wants to merge 11 commits into from
Closed

Add global state management #187

wants to merge 11 commits into from

Conversation

Totodore
Copy link
Owner

@Totodore Totodore commented Dec 5, 2023

This PR adds global state management as well as a State extractor to get the state in any handlers.

Note : Before merging this PR, it is necessary to use dynamics handler for the on_disconnect function. Otherwise it wont be possible to use the state in these handlers.

Currently it is not possible to use the references in extractors because of the following problem :
In the ConnectHandler implementation below for any function with arguments that implements FromConnectParts and are specified by values. To add a lifetime to extractors we could add a lifetime on the trait FromConnectParts. The problem is that the provided lifetime should be the one declared with the for<'a> corresponding to the function and its scope is rather limited.

impl<A, F, $($ty,)*> ConnectHandler<A, (private::Sync, $($ty,)*)> for F
        where
            F: for<'a> FnOnce($($ty,)*) + Send + Sync + Clone + 'static,  // The lifetime 'a should be propagated to the `FromConnectParts` 
            A: Adapter,                                                   // ---------------------------------------
            $( $ty: FromConnectParts<'a, A> + Send, )*                    // <----- here
        {
            fn call(
                &self,
                s: Arc<Socket<A>>,
                auth: Option<String>,
                state: &Arc<dyn std::any::Any + Send + Sync>)
            {
                $(
                    let $ty = match $ty::from_connect_parts(&s, &auth,  state) {
                        Ok(v) => v,
                        Err(_e) => {
                            #[cfg(feature = "tracing")]
                            tracing::error!("Error while extracting data: {}", _e);
                            return;
                        },
                    };
                )*

                (self.clone())($($ty,)*);
            }
        }

Because of this issue any given state must be clonable so it can be given by value to the handlers.

Close :

@Totodore Totodore self-assigned this Dec 5, 2023
@Totodore Totodore linked an issue Dec 5, 2023 that may be closed by this pull request
@tausifcreates
Copy link
Contributor

tausifcreates commented Dec 5, 2023

States are cheap to clone right? So this should be fine. 😄

@Totodore
Copy link
Owner Author

Totodore commented Dec 5, 2023

No, it depends if the state is put in an Arc or not.
State structs are provided by the user and could be any size. Moreover they need to be Sync so that the state is shared and not copied.

@tausifcreates
Copy link
Contributor

Oh, great! I assumed you were going to implement clone-able state because of the said limitations 😄

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clippy found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@Totodore
Copy link
Owner Author

Superseded by #194

@Totodore Totodore closed this Dec 11, 2023
@Totodore Totodore deleted the feat-state-management branch December 15, 2023 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

State Management
2 participants