pub struct Workspace<'ctx> {
pub raw: *mut ws,
/* private fields */
}Expand description
A workspace object
Used to allocate memory in an efficient manner, data will live there until the end of the transaction and the workspace is wiped, so there’s no need to free the objects living in it.
The workspace is usually a few tens of kilobytes large, don’t be greedy. If you need more
space, consider storing your data in a #[shared_per_task] or #[shared_per_vcl] objects.
Fields§
§raw: *mut wsRaw pointer to the C struct
Implementations§
Source§impl<'ctx> Workspace<'ctx>
impl<'ctx> Workspace<'ctx>
Sourcepub unsafe fn alloc(&mut self, size: NonZeroUsize) -> *mut c_void
pub unsafe fn alloc(&mut self, size: NonZeroUsize) -> *mut c_void
Sourcepub fn contains(&self, data: &[u8]) -> bool
pub fn contains(&self, data: &[u8]) -> bool
Check if a pointer is part of the current workspace
Sourcepub fn allocate(
&mut self,
size: NonZeroUsize,
) -> Result<&'ctx mut [MaybeUninit<u8>], VclError>
pub fn allocate( &mut self, size: NonZeroUsize, ) -> Result<&'ctx mut [MaybeUninit<u8>], VclError>
Allocate [u8; size] array on Workspace.
Returns a reference to uninitialized buffer, or an out of memory error.
Sourcepub fn allocate_zeroed(
&mut self,
size: NonZeroUsize,
) -> Result<&'ctx mut [u8], VclError>
pub fn allocate_zeroed( &mut self, size: NonZeroUsize, ) -> Result<&'ctx mut [u8], VclError>
Allocate [u8; size] array on Workspace, and zero it.
Sourcepub fn copy_blob(
&mut self,
value: impl AsRef<[u8]>,
) -> Result<VCL_BLOB, VclError>
pub fn copy_blob( &mut self, value: impl AsRef<[u8]>, ) -> Result<VCL_BLOB, VclError>
Copy any AsRef<[u8]> into a new VCL_BLOB stored in the workspace
Sourcepub fn copy_txt(&mut self, value: impl AsRef<CStr>) -> Result<txt, VclError>
pub fn copy_txt(&mut self, value: impl AsRef<CStr>) -> Result<txt, VclError>
Copy any AsRef<CStr> into a new txt stored in the workspace
Sourcepub fn copy_cstr(
&mut self,
value: impl AsRef<CStr>,
) -> Result<VCL_STRING, VclError>
pub fn copy_cstr( &mut self, value: impl AsRef<CStr>, ) -> Result<VCL_STRING, VclError>
Copy any AsRef<CStr> into a new VCL_STRING stored in the workspace
Sourcepub fn copy_bytes_with_null(
&mut self,
src: impl AsRef<[u8]>,
) -> Result<txt, VclError>
pub fn copy_bytes_with_null( &mut self, src: impl AsRef<[u8]>, ) -> Result<txt, VclError>
Same as Workspace::copy_blob, copying bytes into Workspace, but treats bytes
as a string with an optional NULL character at the end. A NULL is added if it is missing.
Returns an error if src contain NULL characters in a non-last position.
Sourcepub fn vcl_string_builder(&mut self) -> VclResult<WsStrBuffer<'ctx>>
pub fn vcl_string_builder(&mut self) -> VclResult<WsStrBuffer<'ctx>>
Allocate workspace free memory as a string buffer until WsStrBuffer::finish()
is called, resulting in an unsafe VCL_STRING that can be returned to Varnish.
Note that it is possible for the returned buf size to be zero, which
would result in a zero-length nul-terminated VCL_STRING if finished.
Sourcepub fn vcl_blob_builder(&mut self) -> VclResult<WsBlobBuffer<'ctx>>
pub fn vcl_blob_builder(&mut self) -> VclResult<WsBlobBuffer<'ctx>>
Allocate workspace free memory as a byte buffer until WsBlobBuffer::finish()
is called, resulting in an unsafe VCL_BLOB that can be returned to Varnish.
Sourcepub fn slice_builder<T: Copy>(&mut self) -> VclResult<WsTempBuffer<'ctx, T>>
pub fn slice_builder<T: Copy>(&mut self) -> VclResult<WsTempBuffer<'ctx, T>>
Allocate workspace free memory as a temporary vector-like buffer
until WsTempBuffer::finish() is called. The buffer is not intended
to be returned to Varnish, but may be shared among context users.
The buffer is returned as a &'ws [T] to allow mutable access,
while tying the lifetime to the workspace.