Workspace

Struct Workspace 

Source
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 ws

Raw pointer to the C struct

Implementations§

Source§

impl<'ctx> Workspace<'ctx>

Source

pub unsafe fn alloc(&mut self, size: NonZeroUsize) -> *mut c_void

Allocate a buffer of a given size.

§Safety

Allocated memory is not initialized.

Source

pub fn contains(&self, data: &[u8]) -> bool

Check if a pointer is part of the current workspace

Source

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.

Source

pub fn allocate_zeroed( &mut self, size: NonZeroUsize, ) -> Result<&'ctx mut [u8], VclError>

Allocate [u8; size] array on Workspace, and zero it.

Source

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

Source

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

Source

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

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<'ctx> Debug for Workspace<'ctx>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'ctx> Freeze for Workspace<'ctx>

§

impl<'ctx> RefUnwindSafe for Workspace<'ctx>

§

impl<'ctx> !Send for Workspace<'ctx>

§

impl<'ctx> !Sync for Workspace<'ctx>

§

impl<'ctx> Unpin for Workspace<'ctx>

§

impl<'ctx> UnwindSafe for Workspace<'ctx>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.