Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
/// A wrapper type to construct uninitialized instances of `T`. pub inline fn MaybeUninit(comptime T: type) type { return packed union { value: T, uninit: void, const Self = @This(); /// Creates a new initialized `MaybeUninit(T)` initialized with the given value. inline fn init(value: T) Self { return Self{ .value = value }; } /// Creates a new `MaybeUninit(T)` in an uninitialized state. inline fn uninit() Self { return Self{ .uninit = {} }; } /// Creates a new `MaybeUnint(T)` in an uninitialized state, with the memory being filled with `0` bytes. /// It depends on `T` whether that already makes for proper initialization. inline fn zeroed() Self { var u = Self.uninit(); var bytes = @ptrCast([*]u8, u.as_mut_ptr()); @memset(bytes, 0, @sizeOf(T)); return u; } /// Gets a pointer to the contained value. inline fn as_ptr(self: *const Self) *const T { return &self.value; } /// Gets a mutable pointer to the contained value inline fn as_mut_ptr(self: *Self) *T { return &self.value; } /// Extracts the value from the `MaybeUninit(T)` container. inline fn assume_init(self: Self) T { return self.value; } /// Reads the value from the `MabeUninit(T)` container. /// Whenever possible, prefer to use [`MaybeUninit::assume_init`] instead, which prevents duplicating the content of the `MaybeUninit(T)`. inline fn read(self: *const Self) T { return self.as_ptr().*; } /// Sets the value of the `MaybeUninit(T)`. inline fn write(self: *Self, value: T) void { self.value = value; } /// Gets a pointer to the first element of the slice. inline fn first_ptr(this: []const Self) *const T { return @ptrCast(*const T, this.ptr); } /// Gets a mutable pointer to the first element of the slice. inline fn first_ptr_mut(this: []Self) *Self { return @ptrCast(*T, this.ptr); } }; } const List = @import("std").ArrayList(i32); export fn blah() MaybeUninit(List) { return MaybeUninit(List).uninit(); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree