WarnManager | Blazing-Fast Warn messages

WarnManager

Best Warn solution for every project

Magic Code
--!strict
--!optimize 2
--!native

-- Types
type Warner<A...> = {
	Warnln: (Self: Warner<A...>, A...) -> (),
	WarnlnAsync: (Self: Warner<A...>, A...) -> (),
	Warnf: (Self: Warner<A...>, formated: string, A...) -> (),
	WarnlnParallel: (Self: Warner<A...>, A...) -> (),
	Destroy: (Self: Warner<A...>) -> (),
	
	ID: number
}

local NULL = table.freeze({})

-- SoA Arrays
local Printers  = table.create(10, NULL)
local Count     = 0
local FreeList  = table.create(10, NULL) :: {number | {}}
local LastFree  = 0
local TablePool = table.create(10, NULL) do
	for i = 1, 10 do
		TablePool[i] = table.create(10)
	end
end
local LastTable = 10

-- Table {Pool}
local Borrow = function(size: number): {}
	if LastTable > 0 then
		local ret = TablePool[LastTable]
		table.clear(ret)
		LastTable -= 1
		return ret
	else
		return table.create(size)
	end
end

local Free = function(free: {})
	LastTable += 1
	TablePool[LastTable] = free
end

--[[
	@brief Sync version of WarnlnAsync.
	
	@param Self Warner<A...>
	@param A... - The types to warn
--]]
local Warnln = function<A...>(Self: Warner<A...>, ...: A...)
	warn(...)
end

--[[
	@brief warns a line asynchronously.
	
	@param Self Warner<A...>
	@param A... - The types to warn
--]]
local WarnlnAsync = function<A...>(Self: Warner<A...>, ...: A...)
	task.spawn(Warnln, Self, ...)
end

--[[
	@brief warns formated string.

	@param Self Warner<A...>
	@param formated string - The formated string
	@param A... - The types to warn
--]]
local Warnf = function<A...>(Self: Warner<A...>, formated: string, ...: A...)
	warn(string.format(formated :: any, ...))
end


--[[
	@brief Prints a line asynchronously in parallel.
	
	@param Self Warner<A...>
	@param A... - The types to warn
--]]
local WarnlnParallel = function<A...>(Self: Warner<A...>, ...: A...)
	task.desynchronize()
	warn(...)
	task.synchronize()	
end

--[[
	@brief Destroys a warner.
	
	@param Self Printer<A...>
--]]
local Destroy = function<A...>(Self: Warner<A...>)
	local ID = Self.ID
	
	Printers[ID] = NULL
	LastFree += 1
	FreeList[LastFree] = ID
	
	Free(Self)
end

--[[
	@brief Returns a warner
	
	@param A... - The types to print
	@returns Printer<A...>
	
	@usage:
	```
	local Printer = PrintManager()
	
	Printer:Warnln("Hello, world!")
	```
--]]
local MakeWarner = function<A...>(): Warner<A...>
	local ID: number
	if LastFree > 0 then
		FreeList[LastFree] = NULL
		ID = LastFree
		LastFree -= 1
	else
		Count += 1
		ID = Count
	end
	
	local Self = Borrow(6) :: Warner<A...>
	Self.Warnln = Warnln
	Self.WarnlnAsync = WarnlnAsync
	Self.Warnf = Warnf
	Self.WarnlnParallel = WarnlnParallel
	Self.Destroy = Destroy
	Self.ID = ID
	
	Printers[ID] = Self
	
	return Self
end

return MakeWarner

API:

"Warnf"            - "warns formated string"
"Warnln"            - "warns line"
"WarnlnAsync"       - "Prints line asynchronously" 
"Destroy"           - "Destroys waner"
"WarnlnParallel"     - "warns line on other threads!"

Example:

local WarnManager = require(Path.to.WarnManager)

local Warner = WarnManager<<string>>()

Printer:Warnf("Hello %s!", "World") -- AUTO COMPLETE!

:star_struck: Features:

  • Generics support, type safety.
  • Free list for no memory fragmentation
  • SoA Arrays
  • Table pool

My other projects:

1 Like

another unoriginal, boring, useless troll post in community resources..

5 Likes

you’re just mad that you didn’t think about this idea first