cloud_sdk/transport/response/
attempt.rs1use super::{ResponseMetadata, ResponseWriter, ResponseWriterError};
2use crate::transport::{ResponseHeaders, StatusCode};
3
4pub struct ResponseCompletion {
9 status: StatusCode,
10 initialized_len: usize,
11 metadata: ResponseMetadata,
12}
13
14impl ResponseCompletion {
15 #[must_use]
17 pub const fn new(
18 status: StatusCode,
19 initialized_len: usize,
20 metadata: ResponseMetadata,
21 ) -> Self {
22 Self {
23 status,
24 initialized_len,
25 metadata,
26 }
27 }
28
29 #[must_use]
31 pub const fn status(&self) -> StatusCode {
32 self.status
33 }
34}
35
36pub struct AsyncResponseStaging<'staging, 'buffer> {
49 writer: &'staging mut ResponseWriter<'buffer>,
50}
51
52impl<'buffer> AsyncResponseStaging<'_, 'buffer> {
53 #[must_use]
55 pub const fn body_capacity(&self) -> usize {
56 self.writer.body_capacity()
57 }
58
59 pub fn body_mut(&mut self) -> Result<&mut [u8], ResponseWriterError> {
61 self.writer.body_mut()
62 }
63
64 pub fn headers_mut(&mut self) -> Result<&mut ResponseHeaders<'buffer>, ResponseWriterError> {
66 self.writer.headers_mut()
67 }
68
69 #[must_use]
71 pub const fn headers(&self) -> &ResponseHeaders<'buffer> {
72 self.writer.headers()
73 }
74}
75
76pub struct ResponseAttempt<'writer, 'buffer> {
78 pub(super) writer: &'writer mut ResponseWriter<'buffer>,
79 pub(super) completed: bool,
80}
81
82impl<'buffer> ResponseAttempt<'_, 'buffer> {
83 #[must_use]
85 pub const fn body_capacity(&self) -> usize {
86 self.writer.body_capacity()
87 }
88
89 pub fn body_mut(&mut self) -> Result<&mut [u8], ResponseWriterError> {
91 self.writer.body_mut()
92 }
93
94 pub fn headers_mut(&mut self) -> Result<&mut ResponseHeaders<'buffer>, ResponseWriterError> {
96 self.writer.headers_mut()
97 }
98
99 #[must_use]
101 pub const fn headers(&self) -> &ResponseHeaders<'buffer> {
102 self.writer.headers()
103 }
104
105 pub fn commit(
107 &mut self,
108 status: StatusCode,
109 initialized_len: usize,
110 metadata: ResponseMetadata,
111 ) -> Result<(), ResponseWriterError> {
112 self.writer.commit(status, initialized_len, metadata)?;
113 self.completed = true;
114 Ok(())
115 }
116
117 pub(crate) fn staging(&mut self) -> AsyncResponseStaging<'_, 'buffer> {
118 AsyncResponseStaging {
119 writer: self.writer,
120 }
121 }
122
123 pub fn commit_completion(
125 &mut self,
126 completion: ResponseCompletion,
127 ) -> Result<(), ResponseWriterError> {
128 self.commit(
129 completion.status,
130 completion.initialized_len,
131 completion.metadata,
132 )
133 }
134}
135
136impl Drop for ResponseAttempt<'_, '_> {
137 fn drop(&mut self) {
138 if !self.completed {
139 self.writer.rollback_attempt();
140 }
141 }
142}