private unsafe IMemoryOwner <byte> SerializeImpl <T>(T wire) where T : IMessage <T> { int length = wire.CalculateSize(); IMemoryOwner <byte> rented = _pool.RentExact(length); // https://github.com/dotnet/corefx/pull/32669#issuecomment-429579594 fixed(byte *ptr = &MemoryMarshal.GetReference(rented.Memory.Span)) { using (var strm = new UnmanagedMemoryStream(ptr, length, rented.Memory.Length, FileAccess.Write)) { wire.WriteTo(strm); } } return(rented); }