Class UnsafeUtilityExtensions | Collections | 0.15.0-preview.21
docs.unity3d.com
    Show / Hide Table of Contents

    Class UnsafeUtilityExtensions

    Unsafe utility extensions.

    Inheritance
    Object
    UnsafeUtilityExtensions
    Namespace: Unity.Collections.LowLevel.Unsafe
    Syntax
    public static class UnsafeUtilityExtensions

    Methods

    AddressOf<T>(T)

    Return the address of the read-only "in" reference parameter.

    Declaration
    public static void *AddressOf<T>(in T item)
        where T : struct
    Parameters
    Type Name Description
    T item

    The read-only reference to a valuetype of type T.

    Returns
    Type Description
    Void*
    Type Parameters
    Name Description
    T

    Type of the parameter.

    AsRef<T>(T)

    Erases the "read-only" "in" part of the given reference argument, and returns a regular ref to it. Useful to avoid a defensive copy when calling methods on "in" args. Be careful not to mutate the reference target, as doing so may break assumptions the runtime makes.

    Declaration
    public static T AsRef<T>(in T item)
        where T : struct
    Parameters
    Type Name Description
    T item

    The read-only reference to a valuetype of type T.

    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T

    Type of the parameter.

    ReadArrayElementBoundsChecked<T>(Void*, Int32, Int32)

    Reads an element to an unsafe buffer after bounds checking.

    Declaration
    public static T ReadArrayElementBoundsChecked<T>(void *source, int index, int capacity)
    Parameters
    Type Name Description
    Void* source

    Source memory pointer.

    Int32 index

    Index into array.

    Int32 capacity

    Array capacity, used for bounds checking.

    Returns
    Type Description
    T

    Element read from the array.

    Type Parameters
    Name Description
    T

    Type of data in the array.

    Remarks

    Reading data out of bounds from an unsafe buffer can lead to crashes and data corruption. Unity.Collections.LowLevel.Unsafe.UnsafeUtility.ReadArrayElement``1(System.Void*,System.Int32) does not do any bounds checking, so it's fast, but provides no debugging or safety capabilities. This function provides basic bounds checking for Unity.Collections.LowLevel.Unsafe.UnsafeUtility.ReadArrayElement``1(System.Void*,System.Int32) and should be used when debuggability is required over performance.

    Exceptions
    Type Condition
    IndexOutOfRangeException

    Thrown if reading outside of the array's range.

    WriteArrayElementBoundsChecked<T>(Void*, Int32, T, Int32)

    Writes an element to an unsafe buffer after bounds checking.

    Declaration
    public static void WriteArrayElementBoundsChecked<T>(void *destination, int index, T value, int capacity)
    Parameters
    Type Name Description
    Void* destination

    Destination memory pointer.

    Int32 index

    Index into array.

    T value

    Value to write into array.

    Int32 capacity

    Array capacity, used for bounds checking.

    Type Parameters
    Name Description
    T

    Type of data in the array.

    Remarks

    Writing data out of bounds to an unsafe buffer can lead to crashes and data corruption. Unity.Collections.LowLevel.Unsafe.UnsafeUtility.WriteArrayElement``1(System.Void*,System.Int32,``0) does not do any bounds checking, so it's fast, but provides no debugging or safety capabilities. This function provides basic bounds checking for Unity.Collections.LowLevel.Unsafe.UnsafeUtility.WriteArrayElement``1(System.Void*,System.Int32,``0) and should be used when debuggability is required over performance.

    Exceptions
    Type Condition
    IndexOutOfRangeException

    Thrown if element would be written outside of the array's range.

    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023