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 BoxCloneSyncService #777

Merged
merged 4 commits into from
Dec 10, 2024
Merged

Conversation

tthebst
Copy link
Contributor

@tthebst tthebst commented Jul 22, 2024

resolves #770

@jlizen
Copy link
Contributor

jlizen commented Dec 7, 2024

Thanks for cutting this! This is useful in an issue i'm working on for reqwest, to expose the tower stack:
seanmonstar/reqwest#2490 (comment)

Will ping in the tokio/#tower-dev discord to try to help find a reviewer.

@jlizen
Copy link
Contributor

jlizen commented Dec 8, 2024

@tthebst do you see a reason we couldn't add BoxCloneSyncServiceLayer similar to BoxCloneServiceLayer?

Seems fine out of box to me:

use crate::boxed_clone_sync::BoxCloneSyncService;
use std::{fmt, sync::Arc};
use tower_layer::{layer_fn, Layer};
use tower_service::Service;

pub struct BoxCloneSyncServiceLayer<In, T, U, E> {
    boxed: Arc<dyn Layer<In, Service = BoxCloneSyncService<T, U, E>> + Send + Sync + 'static>,
}

impl<In, T, U, E> BoxCloneSyncServiceLayer<In, T, U, E> {
    /// Create a new [`BoxCloneServiceLayer`].
    pub fn new<L>(inner_layer: L) -> Self
    where
        L: Layer<In> + Send + Sync + 'static,
        L::Service: Service<T, Response = U, Error = E> + Send + Sync + Clone + 'static,
        <L::Service as Service<T>>::Future: Send + 'static,
    {
        let layer = layer_fn(move |inner: In| {
            let out = inner_layer.layer(inner);
            BoxCloneSyncService::new(out)
        });

        Self {
            boxed: Arc::new(layer),
        }
    }
}

impl<In, T, U, E> Layer<In> for BoxCloneSyncServiceLayer<In, T, U, E> {
    type Service = BoxCloneSyncService<T, U, E>;

    fn layer(&self, inner: In) -> Self::Service {
        self.boxed.layer(inner)
    }
}

impl<In, T, U, E> Clone for BoxCloneSyncServiceLayer<In, T, U, E> {
    fn clone(&self) -> Self {
        Self {
            boxed: Arc::clone(&self.boxed),
        }
    }
}

impl<In, T, U, E> fmt::Debug for BoxCloneSyncServiceLayer<In, T, U, E> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("BoxCloneServiceLayer").finish()
    }
}

Copy link
Collaborator

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

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

Excellent work, thank you!

@seanmonstar seanmonstar merged commit da24532 into tower-rs:master Dec 10, 2024
14 checks passed
seanmonstar pushed a commit that referenced this pull request Dec 10, 2024
@yyaroshevich
Copy link

Should this one also be updated to be considered for merging? #746

@yyaroshevich
Copy link

Does it make sense to extend ServiceExt to include helper method to construct new service wrapper? The same seem to be also relevant for an existing UnsyncBoxService wrapper.

@jlizen
Copy link
Contributor

jlizen commented Dec 17, 2024

Does it make sense to extend ServiceExt to include helper method to construct new service wrapper? The same seem to be also relevant for an existing UnsyncBoxService wrapper.

Sure, I can cut that tomorrow unless you have time to do it now. Was meaning to but slipped my mind.

Regarding #746 , you could probably ping for a reviewer in the tokio discord's tower-dev channel.

@yyaroshevich
Copy link

Sure, I can cut that tomorrow unless you have time to do it now. Was meaning to but slipped my mind.

No rush, just was curious maybe there was a reason no to include.

Regarding #746 , you could probably ping for a reviewer in the tokio discord's tower-dev channel.

I'm not the author, just today wanted this to be presented in tower. I'll need to take closer look at existing MR, it look abandoned from the first glance, so maybe it's already need to be adjusted/re-done.

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.

Add BoxCloneSyncService
4 participants