CodeSampleX

Sample

tower-layer 0.3.3: Show that `tower-layer` reference-based stacking can share hidden mutable state across wraps.

Verified sample for cargo tower-layer 0.3.3: Show that `tower-layer` reference-based stacking can share hidden mutable state across wraps. The contract ran…

sha256:67aad743bfcda22dbbef71bd3cc7a9cbf0de2d864ae984e0560355f976360ace

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
3
Signing keys that built it
3
Declared environment rust linux x64 rust rust cargo

Verification-run environments

Environment Contract Stages Run
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-17
rust 1 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-18
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-08

Case

HOW
Goal
Show that `tower-layer` reference-based stacking can share hidden mutable state across wraps.
Packages
Symbols
  • tower_layer::Layer
Environment
rust
Created
2026-08-17T01:08:09Z

Contract

  1. assert that the same borrowed layer can be used for multiple `Layer::layer` calls and the call count is cumulative
  2. assert that wrapped services reflect distinct build IDs because the shared reference updates shared state
  3. assert that this shared mutable behavior is observable even though the trait method takes `&self`

Files

  • Cargo.lock
  • Cargo.toml
  • NOTES.md
  • csx.json
  • src/lib.rs

Download the source artifact (tar.gz)

Source

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "tower-layer"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"

[[package]]
name = "tower-layer-version-trap"
version = "0.1.0"
dependencies = [
 "tower-layer",
]
Cargo.toml
[package]
name = "tower-layer-version-trap"
version = "0.1.0"
edition = "2021"

[dependencies]
tower-layer = "0.3.3"
NOTES.md
Search hit used: sample `sha256:70cb8cec09e074888556da7097e94d68ad979b471ca30473f3e2662c391dd7fd` (ServiceBuilder layering/order and timeout/load-shed/retry behavior), so this sample uses a different trap: shared state from borrowed `&Layer` usages.

In a pre-`0.3.1` `tower-layer`, the borrowed `&Layer` call fails to compile at type-check time (`&T` does not implement `Layer`), while `0.3.3` compiles and accumulates shared state across wraps.
csx.json
{"case":{"believed":"A competent developer would expect a shared `\u0026Layer` to behave like independent applications and not accumulate per-wrap side effects when reused.","caseId":"case:sha256:5d64a65cc29fabbb7950228ab78bcde9443f7e5ee71d09ed358b9013a29da803","contract":["assert that the same borrowed layer can be used for multiple `Layer::layer` calls and the call count is cumulative","assert that wrapped services reflect distinct build IDs because the shared reference updates shared state","assert that this shared mutable behavior is observable even though the trait method takes `\u0026self`"],"goal":"Show that `tower-layer` reference-based stacking can share hidden mutable state across wraps.","kind":"HOW","packages":["pkg:cargo/tower-layer@0.3.3"],"schemaVersion":1,"symbols":["tower_layer::Layer"]},"contractCommand":["cargo","test","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/tower-layer@0.3.3"],"schemaVersion":1,"symbols":["tower_layer::Layer"],"verifierAdapter":"cargo@1"}
src/lib.rs
use std::cell::Cell;
use std::rc::Rc;

use tower_layer::Layer;

#[derive(Clone)]
struct SharedCounterLayer {
    calls: Rc<Cell<usize>>,
}

impl SharedCounterLayer {
    fn new() -> Self {
        Self {
            calls: Rc::new(Cell::new(0)),
        }
    }
}

#[derive(Clone, Debug)]
struct WrappedService<T> {
    inner: T,
    build_id: usize,
}

impl<T> WrappedService<T> {
    fn build_id(&self) -> usize {
        self.build_id
    }
}

impl<T> Layer<T> for SharedCounterLayer {
    type Service = WrappedService<T>;

    fn layer(&self, inner: T) -> Self::Service {
        let build_id = self.calls.get() + 1;
        self.calls.set(build_id);

        WrappedService {
            inner,
            build_id,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn shared_reference_layering_reuses_state() {
        let layer = SharedCounterLayer::new();
        let layer_ref = &layer;

        let first = layer_ref.layer("alpha");
        let second = layer_ref.layer("bravo");

        assert_eq!(layer.calls.get(), 2);
        assert_eq!(first.build_id(), 1);
        assert_eq!(second.build_id(), 2);
        assert_ne!(first.build_id(), second.build_id());

        let third = (&layer).layer("charlie");
        assert_eq!(third.build_id(), 3);
        assert_eq!(layer.calls.get(), 3);
    }
}

Origin Seeder

csx-seed