internal static async Task <object> ToObjectAsync(this RpcSharedMemory sharedMem, ILogger logger, string invocationId, ISharedMemoryManager sharedMemoryManager, bool isFunctionDataCacheEnabled)
        {
            // Data was transferred by the worker using shared memory
            string mapName = sharedMem.Name;
            int    offset  = (int)sharedMem.Offset;
            int    count   = (int)sharedMem.Count;

            logger.LogTrace("Shared memory data transfer for invocation id: {Id} with shared memory map name: {MapName} and size: {Size} bytes", invocationId, mapName, count);

            // If cache is enabled, we hold a reference (in the host) to the memory map created by the worker
            // so that the worker can be asked to drop its reference.
            // We will later add this object into the cache and then the memory map will be freed as per the eviction logic of the cache.
            if (isFunctionDataCacheEnabled)
            {
                switch (sharedMem.Type)
                {
                case RpcDataType.Bytes:
                case RpcDataType.String:
                    // This is where the SharedMemoryManager will hold a reference to the memory map
                    if (sharedMemoryManager.TryTrackSharedMemoryMap(mapName))
                    {
                        return(await sharedMemoryManager.GetObjectAsync(mapName, offset, count, typeof(SharedMemoryObject)));
                    }
                    break;

                default:
                    logger.LogError("Unsupported shared memory data type: {SharedMemDataType} with FunctionDataCache for invocation id: {Id}", sharedMem.Type, invocationId);
                    throw new InvalidDataException($"Unsupported shared memory data type with FunctionDataCache: {sharedMem.Type}");
                }
            }

            // If the cache is not used, we copy the object content from the memory map so that the worker
            // can be asked to drop the reference to the memory map.
            switch (sharedMem.Type)
            {
            case RpcDataType.Bytes:
                return(await sharedMemoryManager.GetObjectAsync(mapName, offset, count, typeof(byte[])));

            case RpcDataType.String:
                return(await sharedMemoryManager.GetObjectAsync(mapName, offset, count, typeof(string)));

            default:
                logger.LogError("Unsupported shared memory data type: {SharedMemDataType} for invocation id: {Id}", sharedMem.Type, invocationId);
                throw new InvalidDataException($"Unsupported shared memory data type: {sharedMem.Type}");
            }
        }
        internal static async Task <object> ToObjectAsync(this RpcSharedMemory sharedMem, ILogger logger, string invocationId, ISharedMemoryManager sharedMemoryManager)
        {
            // Data was transferred by the worker using shared memory
            string mapName = sharedMem.Name;
            int    offset  = (int)sharedMem.Offset;
            int    count   = (int)sharedMem.Count;

            logger.LogTrace("Shared memory data transfer for invocation id: {Id} with shared memory map name: {MapName} and size: {Size} bytes", invocationId, mapName, count);

            switch (sharedMem.Type)
            {
            case RpcDataType.Bytes:
                return(await sharedMemoryManager.GetObjectAsync(mapName, offset, count, typeof(byte[])));

            case RpcDataType.String:
                return(await sharedMemoryManager.GetObjectAsync(mapName, offset, count, typeof(string)));

            default:
                logger.LogError("Unsupported shared memory data type: {SharedMemDataType} for invocation id: {Id}", sharedMem.Type, invocationId);
                throw new InvalidDataException($"Unsupported shared memory data type: {sharedMem.Type}");
            }
        }