|
| 1 | +// This is a customer's code that would break when RuntimeApiClientFuture |
| 2 | +// changes from RuntimeApiClientFuture<F> to RuntimeApiClientFuture<F, B> |
| 3 | + |
| 4 | +use lambda_runtime::{service_fn, Error, LambdaEvent, Runtime}; |
| 5 | +use serde_json::Value; |
| 6 | +use tower::{Layer, Service}; |
| 7 | +use std::task::{Context, Poll}; |
| 8 | +use std::future::Future; |
| 9 | +use std::pin::Pin; |
| 10 | + |
| 11 | +// Customer creates a custom middleware layer |
| 12 | +pub struct LoggingLayer; |
| 13 | + |
| 14 | +impl<S> Layer<S> for LoggingLayer { |
| 15 | + type Service = LoggingService<S>; |
| 16 | + |
| 17 | + fn layer(&self, inner: S) -> Self::Service { |
| 18 | + LoggingService { inner } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +pub struct LoggingService<S> { |
| 23 | + inner: S, |
| 24 | +} |
| 25 | + |
| 26 | +// Here's where the breaking change happens! |
| 27 | +// The customer implements Service and explicitly names the Future type |
| 28 | +impl<S> Service<lambda_runtime::LambdaInvocation> for LoggingService<S> |
| 29 | +where |
| 30 | + S: Service<lambda_runtime::LambdaInvocation, Response = ()>, |
| 31 | + S::Error: std::error::Error + Send + Sync + 'static, |
| 32 | + S::Future: Future<Output = Result<(), S::Error>> + Send + 'static, |
| 33 | +{ |
| 34 | + type Response = (); |
| 35 | + type Error = S::Error; |
| 36 | + |
| 37 | + // BREAKING CASE 1: Wrapping the future with explicit type annotation |
| 38 | + type Future = Pin<Box<dyn Future<Output = Result<(), S::Error>> + Send>>; |
| 39 | + |
| 40 | + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 41 | + self.inner.poll_ready(cx) |
| 42 | + } |
| 43 | + |
| 44 | + fn call(&mut self, req: lambda_runtime::LambdaInvocation) -> Self::Future { |
| 45 | + println!("Processing invocation: {}", req.context.request_id); |
| 46 | + |
| 47 | + let future = self.inner.call(req); |
| 48 | + |
| 49 | + // BREAKING CASE 2: If customer tries to inspect the future type |
| 50 | + // They might write code that depends on the concrete type |
| 51 | + Box::pin(async move { |
| 52 | + let result = future.await; |
| 53 | + println!("Invocation completed"); |
| 54 | + result |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// BREAKING CASE 3: Customer writes a helper function that explicitly |
| 60 | +// constrains the Future type based on what they observed |
| 61 | +pub fn create_runtime_with_logging<F, EventPayload, Response>( |
| 62 | + handler: F, |
| 63 | +) -> Runtime< |
| 64 | + LoggingService< |
| 65 | + // This type signature explicitly names RuntimeApiClientService |
| 66 | + // and its associated types, which would break when generics change |
| 67 | + impl Service< |
| 68 | + lambda_runtime::LambdaInvocation, |
| 69 | + Response = (), |
| 70 | + Error = lambda_runtime_api_client::BoxError, |
| 71 | + // The Future type here implicitly depends on RuntimeApiClientFuture |
| 72 | + > |
| 73 | + > |
| 74 | +> |
| 75 | +where |
| 76 | + F: Service<LambdaEvent<EventPayload>, Response = Response>, |
| 77 | + F::Future: Future<Output = Result<Response, F::Error>>, |
| 78 | + F::Error: Into<lambda_runtime::Diagnostic> + std::fmt::Debug, |
| 79 | + EventPayload: for<'de> serde::Deserialize<'de>, |
| 80 | + Response: lambda_runtime::IntoFunctionResponse<Value, futures::stream::Empty<Result<bytes::Bytes, std::io::Error>>>, |
| 81 | +{ |
| 82 | + Runtime::new(handler).layer(LoggingLayer) |
| 83 | +} |
| 84 | + |
| 85 | +#[tokio::main] |
| 86 | +async fn main() -> Result<(), Error> { |
| 87 | + let handler = service_fn(my_handler); |
| 88 | + |
| 89 | + let runtime = create_runtime_with_logging(handler); |
| 90 | + |
| 91 | + runtime.run().await?; |
| 92 | + Ok(()) |
| 93 | +} |
| 94 | + |
| 95 | +async fn my_handler(event: LambdaEvent<Value>) -> Result<Value, Error> { |
| 96 | + Ok(event.payload) |
| 97 | +} |
0 commit comments