For rust's standard library, there is RawVec<T, A>
which is the backbone of Vec<T>
defined as:
pub struct RawVec<T, A: Allocator = Global> {
ptr: Unique<T>,
cap: usize,
alloc: A,
}
This struct takes Global
as its default allocator type here.
The first implementation of RawVec
:
impl<T> RawVec<T, Global> {
pub const fn new() -> Self {
Self::new_in(Global)
}
The second:
impl<T, A: Allocator> RawVec<T, A> {
pub const fn new_in(alloc: A) -> Self {
Self { ptr: Unique::dangling(), cap: 0, alloc }
}
}
The second implementation is more generic than the first one in that it tasks account of Allocator
. The first implementation benefits from the second one, and uses Global
as its allocator.
However, what I am wondering here is that Global
is actually a type, not a variable, how does this statement works?
Self::new_in(Global)
To me, this statement passes a type of Global as function parameter, and obviously not possible.