Struct fastly::handle::BodyHandle
source · [−]#[repr(transparent)]pub struct BodyHandle { /* private fields */ }
Expand description
A low-level interface to HTTP bodies.
For most applications, you should use Body
instead of this
interface. See the top-level handle
documentation for more details.
This type implements Read
to read bytes from the beginning of a body, and Write
to write
to the end of a body. Note that these operations are unbuffered, unlike the same operations on
the higher-level Body
type.
Implementations
sourceimpl BodyHandle
impl BodyHandle
sourcepub const INVALID: Self = _
pub const INVALID: Self = _
An invalid body handle.
This is primarily useful to represent uninitialized values when using the interfaces in
fastly_sys
.
sourcepub const fn is_invalid(&self) -> bool
pub const fn is_invalid(&self) -> bool
Returns true
if the body handle is invalid.
sourcepub unsafe fn from_u32(handle: u32) -> Self
pub unsafe fn from_u32(handle: u32) -> Self
Make a handle from its underlying representation.
This should only be used when calling the raw ABI directly, and care should be taken not to reuse or alias handle values.
sourcepub unsafe fn as_u32(&self) -> u32
pub unsafe fn as_u32(&self) -> u32
Get the underlying representation of the handle.
This should only be used when calling the raw ABI directly, and care should be taken not to reuse or alias handle values.
sourcepub unsafe fn as_u32_mut(&mut self) -> &mut u32
pub unsafe fn as_u32_mut(&mut self) -> &mut u32
Get a mutable reference to the underlying u32
representation of the handle.
This should only be used when calling the raw ABI directly, and care should be taken not to reuse or alias handle values.
sourcepub fn into_u32(self) -> u32
pub fn into_u32(self) -> u32
Turn a handle into its representation without closing the underlying resource.
This should only be used when calling the raw ABI directly, and care should be taken not to reuse or alias handle values.
sourcepub fn from_client() -> Self
pub fn from_client() -> Self
Get a handle to the client request body.
This handle may only be retrieved once per execution, either through this function or
through client_request_and_body()
.
sourcepub fn append(&mut self, other: BodyHandle)
pub fn append(&mut self, other: BodyHandle)
Append another body onto the end of this body.
This operation is performed in amortized constant time, and so should always be preferred to reading an entire body and then writing the same contents to another body.
The other body will no longer be valid after this call.
sourcepub fn into_bytes(self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
pub fn into_bytes(self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A>where
A: Allocator,
A: Allocator,
Read the entirety of the body into a byte vector.
Memory usage
This method will cause the entire body to be buffered in WebAssembly memory. You should take
care not to exceed the WebAssembly memory limits, and consider using Read
methods to
control how much of the body you process at once.
sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Read the entirety of the body into a String
, interpreting the bytes as UTF-8.
Memory usage
This method will cause the entire body to be buffered in WebAssembly memory. You should take
care not to exceed the WebAssembly memory limits, and consider using Read
methods to
control how much of the body you process at once.
Panics
If the body does not contain a valid UTF-8 string, this function will panic. To explicitly handle
the possibility of invalid UTF-8 data, use into_bytes()
and then convert
the bytes explicitly with a function like String::from_utf8
.
sourcepub fn write_bytes(&mut self, bytes: &[u8]) -> usize
pub fn write_bytes(&mut self, bytes: &[u8]) -> usize
Write a slice of bytes to the end of this body, and return the number of bytes written.
Examples
body.write_bytes(&[0, 1, 2, 3]);
sourcepub fn write_str(&mut self, string: &str) -> usize
pub fn write_str(&mut self, string: &str) -> usize
Write a string slice to the end of this body, and return the number of bytes written.
Examples
body.write_str("woof woof");
sourcepub fn close(self) -> Result<(), HandleError>
pub fn close(self) -> Result<(), HandleError>
Close the BodyHandle by removing it from the host Session. This will close out streaming and non streaming bodies. Care must be taken to only close out the handle if the body is not being written to or read from anymore. If the handle has already been closed an error will be returned.
Examples
let mut body = BodyHandle::new();
body.write_str("You're already closed.");
// The handle is not being used in a request and doesn't refer to any
// response body so we can close this out
body.close()?;
Trait Implementations
sourceimpl Debug for BodyHandle
impl Debug for BodyHandle
sourceimpl Drop for BodyHandle
impl Drop for BodyHandle
sourceimpl From<&[u8]> for BodyHandle
impl From<&[u8]> for BodyHandle
sourceimpl From<&str> for BodyHandle
impl From<&str> for BodyHandle
sourceimpl From<BodyHandle> for Body
impl From<BodyHandle> for Body
sourcefn from(handle: BodyHandle) -> Self
fn from(handle: BodyHandle) -> Self
sourceimpl From<String> for BodyHandle
impl From<String> for BodyHandle
sourceimpl Hash for BodyHandle
impl Hash for BodyHandle
sourceimpl PartialEq<BodyHandle> for BodyHandle
impl PartialEq<BodyHandle> for BodyHandle
sourcefn eq(&self, other: &BodyHandle) -> bool
fn eq(&self, other: &BodyHandle) -> bool
sourceimpl Read for BodyHandle
impl Read for BodyHandle
sourcefn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · sourcefn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresourcefn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · sourcefn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
buf
. Read more1.0.0 · sourcefn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · sourcefn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresourcefn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)sourcefn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · sourcefn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
Read
. Read moresourceimpl Write for BodyHandle
impl Write for BodyHandle
sourcefn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
sourcefn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)