Skip to main content

cloud_sdk/operation/prepared/
storage.rs

1use core::fmt;
2
3use super::PreparedRequest;
4
5/// Caller-owned target and request-body storage supplied to preparation.
6pub struct PreparationStorage<'storage> {
7    target: &'storage mut [u8],
8    body: &'storage mut [u8],
9}
10
11impl<'storage> PreparationStorage<'storage> {
12    /// Creates complete caller-owned storage for one preparation attempt.
13    ///
14    /// # Security
15    ///
16    /// Preparation may write credentials or other secrets into `body`. A
17    /// successful [`PreparedRequest`] must retain those bytes until transport
18    /// use, so this wrapper cannot clear them on success. For secret-bearing
19    /// operations, guard `body` with a volatile-clearing type such as
20    /// `cloud_sdk_sanitization::SecretBuffer` and drop the guard immediately
21    /// after transport use. A plain mutable slice is not cleared when the
22    /// prepared request is dropped.
23    #[must_use]
24    pub const fn new(target: &'storage mut [u8], body: &'storage mut [u8]) -> Self {
25        Self { target, body }
26    }
27
28    /// Consumes the storage wrapper and returns both independent buffers.
29    #[must_use]
30    pub fn into_parts(self) -> (&'storage mut [u8], &'storage mut [u8]) {
31        (self.target, self.body)
32    }
33}
34
35impl fmt::Debug for PreparationStorage<'_> {
36    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
37        formatter
38            .debug_struct("PreparationStorage")
39            .field("target_capacity", &self.target.len())
40            .field("body_capacity", &self.body.len())
41            .finish()
42    }
43}
44
45/// Typed provider operation that can prepare one complete request.
46///
47/// ```compile_fail
48/// use cloud_sdk::operation::PrepareOperation;
49///
50/// fn prepare_without_storage<O: PrepareOperation>(operation: &O) {
51///     let _ = operation.prepare();
52/// }
53/// ```
54pub trait PrepareOperation {
55    /// Preparation-specific failure.
56    type Error;
57
58    /// Writes into caller storage and returns an executable prepared request.
59    fn prepare<'storage>(
60        &self,
61        storage: PreparationStorage<'storage>,
62    ) -> Result<PreparedRequest<'storage>, Self::Error>;
63}