Skip to main content

ResourceLimits

Struct ResourceLimits 

Source
#[non_exhaustive]
pub struct ResourceLimits { pub max_pixels: Option<u64>, pub max_memory_bytes: Option<u64>, pub max_output_bytes: Option<u64>, pub max_width: Option<u32>, pub max_height: Option<u32>, pub max_input_bytes: Option<u64>, pub max_frames: Option<u32>, pub max_animation_ms: Option<u64>, pub threading: ThreadingPolicy, }
Expand description

Resource limits for encode/decode operations.

Used to prevent DoS attacks and resource exhaustion. All fields are optional; None means no limit for that resource.

Codecs enforce what they can — not all codecs support all limit types. Use the check_* methods for caller-side validation before decode/encode.

§Example

use zencodec::ResourceLimits;

let limits = ResourceLimits::none()
    .with_max_pixels(100_000_000)
    .with_max_memory(512 * 1024 * 1024);

Typical usage with a decoder:

// Parse-time rejection (before any pixel work)
let info = config.probe_header(data)?;
limits.check_image_info(&info)?;

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§max_pixels: Option<u64>

Maximum total pixels (width × height).

§max_memory_bytes: Option<u64>

Maximum memory allocation in bytes.

§max_output_bytes: Option<u64>

Maximum encoded output size in bytes (encode only).

§max_width: Option<u32>

Maximum image width in pixels.

§max_height: Option<u32>

Maximum image height in pixels.

§max_input_bytes: Option<u64>

Maximum input data size in bytes (decode only).

§max_frames: Option<u32>

Maximum number of animation frames.

§max_animation_ms: Option<u64>

Maximum total animation duration in milliseconds.

§threading: ThreadingPolicy

Threading policy for the codec.

Defaults to ThreadingPolicy::Unlimited.

Implementations§

Source§

impl ResourceLimits

Source

pub fn none() -> Self

No limits (all fields None), unlimited threading.

Source

pub fn with_max_pixels(self, max: u64) -> Self

Set maximum total pixels.

Source

pub fn with_max_memory(self, bytes: u64) -> Self

Set maximum memory allocation in bytes.

Source

pub fn with_max_output(self, bytes: u64) -> Self

Set maximum encoded output size in bytes.

Source

pub fn with_max_width(self, width: u32) -> Self

Set maximum image width in pixels.

Source

pub fn with_max_height(self, height: u32) -> Self

Set maximum image height in pixels.

Source

pub fn with_max_input_bytes(self, bytes: u64) -> Self

Set maximum input data size in bytes (decode only).

Source

pub fn with_max_frames(self, frames: u32) -> Self

Set maximum number of animation frames.

Source

pub fn with_max_animation_ms(self, ms: u64) -> Self

Set maximum total animation duration in milliseconds.

Source

pub fn with_threading(self, policy: ThreadingPolicy) -> Self

Set threading policy.

Source

pub fn threading(&self) -> ThreadingPolicy

Current threading policy.

Source

pub fn has_any(&self) -> bool

Whether any limits are set (including non-default threading).

Source

pub fn check_dimensions( &self, width: u32, height: u32, ) -> Result<(), LimitExceeded>

Check image dimensions against max_width, max_height, and max_pixels.

Source

pub fn check_memory(&self, bytes: u64) -> Result<(), LimitExceeded>

Check a memory estimate against max_memory_bytes.

Source

pub fn check_input_size(&self, bytes: u64) -> Result<(), LimitExceeded>

Check input data size against max_input_bytes.

Source

pub fn check_output_size(&self, bytes: u64) -> Result<(), LimitExceeded>

Check encoded output size against max_output_bytes.

Source

pub fn check_frames(&self, count: u32) -> Result<(), LimitExceeded>

Check frame count against max_frames.

Source

pub fn check_animation_ms(&self, ms: u64) -> Result<(), LimitExceeded>

Check animation duration against max_animation_ms.

Source

pub fn check_image_info(&self, info: &ImageInfo) -> Result<(), LimitExceeded>

Check ImageInfo from probe_header() against all applicable limits. This is the fastest rejection point — call it immediately after probing, before any pixel work.

Checks: max_width, max_height, max_pixels, max_frames.

Source

pub fn check_output_info(&self, info: &OutputInfo) -> Result<(), LimitExceeded>

Check OutputInfo against dimension limits.

Checks: max_width, max_height, max_pixels.

Trait Implementations§

Source§

impl Clone for ResourceLimits

Source§

fn clone(&self) -> ResourceLimits

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ResourceLimits

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ResourceLimits

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for ResourceLimits

Source§

fn eq(&self, other: &ResourceLimits) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for ResourceLimits

Source§

impl Eq for ResourceLimits

Source§

impl StructuralPartialEq for ResourceLimits

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.