You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: command_queue.rs
    path of file: ./repos/bevy/crates/bevy_ecs/src/world
    
    the code of the file until where you have to start completion: use crate::system::{SystemBuffer, SystemMeta};

use std::{fmt::Debug, mem::MaybeUninit};

use bevy_ptr::{OwningPtr, Unaligned};
use bevy_utils::tracing::warn;

use crate::world::{Command, World};

struct CommandMeta {
    /// SAFETY: The `value` must point to a value of type `T: Command`,
    /// where `T` is some specific type that was used to produce this metadata.
    ///
    /// `world` is optional to allow this one function pointer to perform double-duty as a drop.
    ///
    /// Returns the size of `T` in bytes.
    consume_command_and_get_size:
        unsafe fn(value: OwningPtr<Unaligned>, world: Option<&mut World>) -> usize,
}

/// Densely and efficiently stores a queue of heterogenous types implementing [`Command`].
//
// NOTE: [`CommandQueue`] is implemented via a `Vec<MaybeUninit<u8>>` instead of a `Vec<Box<dyn Command>>`
// as an optimization. Since commands are used frequently in systems as a way to spawn
// entities/components/resources, and it's not currently possible to parallelize these
// due to mutable [`World`] access, maximizing performance for [`CommandQueue`] is
// preferred to simplicity of implementation.
#[derive(Default)]
pub struct CommandQueue {
    // This buffer densely stores all queued commands.
    //
    // For each command, one `CommandMeta` is stored, followed by zero or more bytes
    // to store the command itself. To interpret these bytes, a pointer must
    // be passed to the corresponding `CommandMeta.apply_command_and_get_size` fn pointer.
    bytes: Vec<MaybeUninit<u8>>,
}

// CommandQueue needs to implement Debug manually, rather than deriving it, because the derived impl just prints
// [core::mem::maybe_uninit::MaybeUninit<u8>, core::mem::maybe_uninit::MaybeUninit<u8>, ..] for every byte in the vec,
// which gets extremely verbose very quickly, while also providing no useful information.
// It is not possible to soundly print the values of the contained bytes, as some of them may be padding or uninitialized (#4863)
// So instead, the manual impl just prints the length of vec.
impl Debug for CommandQueue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CommandQueue")
            .field("len_bytes", &self.bytes.len())
            .finish_non_exhaustive()
    }
}

// SAFETY: All commands [`Command`] implement [`Send`]
unsafe impl Send for CommandQueue {}

// SAFETY: `&CommandQueue` never gives access to the inner commands.
unsafe impl Sync for CommandQueue {}

impl CommandQueue {
    /// Push a [`Command`] onto the queue.
    #[inline]
    pub fn push<C>(&mut self, command: C)
    where
        C: Command,
    {
        // Stores a command alongside its metadata.
        // `repr(C)` prevents the compiler from reordering the fields,
        // while `repr(packed)` prevents the compiler from inserting padding bytes.
        #[repr(C, packed)]
        struct Packed<T: Command> {
            meta: CommandMeta,
            command: T,
        }

        let meta = CommandMeta {
            consume_command_and_get_size: |command, world| {
                // SAFETY: According to the invariants of `CommandMeta.consume_command_and_get_size`,
                // `command` must point to a value of type `C`.
                let command: C = unsafe { command.read_unaligned() };
                match world {
                    // Apply command to the provided world...
                    Some(world) => command.apply(world),
                    // ...or discard it.
                    None => drop(command),
                }
                std::mem::size_of::<C>()
            },
        };

        let old_len = self.bytes.len();

        // Reserve enough bytes for both the metadata and the command itself.
        self.bytes.reserve(std::mem::size_of::<Packed<C>>());

        // Pointer to the bytes at the end of the buffer.
        // SAFETY: We know it is within bounds of the allocation, due to the call to `.reserve()`.
        let ptr = unsafe { self.bytes.as_mut_ptr().add(old_len) };

        // Write the metadata into the buffer, followed by the command.
        // We are using a packed struct to write them both as one operation.
        // SAFETY: `ptr` must be non-null, since it is within a non-null buffer.
        // The call to `reserve()` ensures that the
    
    fn apply_or_drop_queued(&mut self, mut world: Option<&mut World>) {
        // The range of pointers of the filled portion of `self.bytes`.
        let bytes_range = self.bytes.as_mut_ptr_range();

        // Pointer that will iterate over the entries of the buffer.
        let cursor = bytes_range.start;

        let end = bytes_range.end;

        // Reset the buffer, so it can be reused after this function ends.
        // In the loop below, ownership of each command will be transferred into user code.
        // SAFETY: `set_len(0)` is always valid.
        unsafe { self.bytes.set_len(0) };

        // Create a stack for the command queue's we will be applying as commands may queue additional commands.
        // This is preferred over recursion to avoid stack overflows.
        let mut resolving_commands = vec![(cursor, end)];
        // Take ownership of any additional buffers so they are not free'd uintil they are iterated.
        let mut buffers = Vec::new();

        // Add any commands in the world's internal queue to the top of the stack.
        if let Some(world) = &mut world {
            if !world.command_queue.is_empty() {
                let mut bytes = std::mem::take(&mut world.command_queue.bytes);
                let bytes_range = bytes.as_mut_ptr_range();
                resolving_commands.push((bytes_range.start, bytes_range.end));
                buffers.push(bytes);
            }
        }

        while let Some((mut cursor, mut end)) = resolving_commands.pop() {
            while cursor < end {
                // SAFETY: The cursor is either at the start of the buffer, or just after the previous command.
                // Since we know that the cursor is in bounds, it must point to the start of a new command.
                let meta = unsafe { cursor.cast::<CommandMeta>().read_unaligned() };
                // Advance to the bytes just after `meta`, which represent a type-erased command.
                // SAFETY: For most types of `Command`, the pointer immediately following the metadata
                // is guaranteed to be in bounds. If the command is a zero-sized type (ZST), then the cursor
                // might be 1 byte past the end of the buffer, which is safe.
                cursor = unsafe { cursor.add(std::mem::size_of::<CommandMeta>()) };
                // Construct an owned pointer to the command.
                // SAFETY: It is safe to transfer ownership out of `self.bytes`, since the call to `set_len(0)` above
                // guarantees that nothing stored in the buffer will get observed after this function ends.
                // `cmd` points to a valid address of a stored command, so it must be non-null.
                let cmd = unsafe {
                    OwningPtr::<Unaligned>::new(std::ptr::NonNull::new_unchecked(cursor.cast()))
                };
                // SAFETY: The data underneath the cursor must correspond to the type erased in metadata,
                // since they were stored next to each other by `.push()`.
                // For ZSTs, the type doesn't matter as long as the pointer is non-null.
                let size =
                    unsafe { (meta.consume_command_and_get_size)(cmd, world.as_deref_mut()) };
                // Advance the cursor past the command. For ZSTs, the cursor will not move.
                // At this point, it will either point to the next `CommandMeta`,
                // or the cursor will be out of bounds and the loop will end.
                // SAFETY: The address just past the command is either within the buffer,
                // or 1 byte past the end, so this addition will not overflow the pointer's allocation.
                cursor = unsafe { cursor.add(size) };

                if let Some(world) = &mut world {
                    // If the command we just applied generated more commands we must apply those first
                    if !world.command_queue.is_empty() {
                        // If our current list of commands isn't complete push it to the `resolving_commands` stack to be applied after
                        if cursor < end {
                            resolving_commands.push((cursor, end));
                        }
                        let mut bytes = std::mem::take(&mut world.command_queue.bytes);

                        // Start applying the new queue
                        let bytes_range = bytes.as_mut_ptr_range();
                        cursor = bytes_range.start;
                        end = bytes_range.end;

                        // Store our buffer so it is not dropped;
                        buffers.push(bytes);
                    }
                }
            }
            // Re-use last buffer to avoid re-allocation
            if let (Some(world), Some(buffer)) = (&mut world, buffers.pop()) {
                world.command_queue.bytes = buffer;
                // SAFETY: `set_len(0)` is always valid.
                unsafe { world.command_queue.bytes.set_len(0) };
            }
        }
    }