Files
sakurac/core/traits/ownership.sakura
T
2026-11-19 04:54:17 -06:00

170 lines
7.1 KiB
Plaintext

#! @file ownership.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the traits saying what becomes of a value: `owning`, which a type
#! names to own what it holds and close it where its values go away, with the
#! function closing a value where it stands; `closeable`, which the compiler
#! gives every type whose values are closed; `copyable`, with `copy`, which
#! copies a value through it, or byte for byte where nothing it holds closes;
#! and `moveable`, with `move`, which moves a value through it, or byte for
#! byte.
module core;
#! Defines that a type owns what it holds, such as memory or a file, and
#! disposes of it in `close`. A value of a type naming `owning` is closed where
#! its scope ends, as RAII does, so that something like an allocator or
#! collection has released its data by the time it goes away.
#!
#! A type is owning only by naming the trait, so a `close` meant to be called by
#! hand never becomes a destructor by accident. A reference to an owning value
#! owns nothing, so it is never owning, though `close` is called through it.
#!
#! Unlike `copyable` and `moveable`, nothing fails where a type is not owning:
#! generic code asks `if const owns::<T>::value`, or calls `close`, which closes
#! whatever is given as its scope ending would.
pub trait owning {
#! Disposes of what the value owns, as its scope ending does.
#! @param this The value to close, which should be left empty, so closing it
#! again does nothing.
def close(&mut this): void;
}
#! Closes a value where it stands, as its scope ending would: through its own
#! `close` where it is `owning`, then each value it holds that closes, such as
#! a `string` in a field, the last first. A value that closes nothing is left
#! alone.
#! @tparam T The type of the value.
#! @param value The value to close, which is left empty.
#! @remarks Containers close their elements with it, so that an element holding
#! something that closes is closed as a local holding it would be.
pub def close<T>(value: &mut T): void {
if const T is closeable {
close_where_it_is(value);
memset(value as *mut u8, 0, size_of::<T>());
}
}
# Closes a value where it is, as the compiler closes a local whose scope ends:
# its own `close` where it is `owning`, then each field that closes, the last
# first, or the field a union holds, or each element of a fixed array.
@intrinsic
def close_where_it_is<T>(value: &mut T): void;
#! A type whose values are closed where they go away: one that is `owning`, or
#! one holding a value that is, in a field or an element.
#!
#! Generic code asks `if const T is closeable` where what it does only matters
#! for values that close, such as emptying the memory a value was moved out of
#! so that it is not closed twice, which plain data has no need of.
#!
#! The compiler decides what is closeable, so no type can implement it.
pub trait closeable {}
#! A type whose values can be copied, by a static function building a copy of
#! another value.
#!
#! The function is static, and gives the copy back rather than filling a
#! destination that already exists, so that returning it writes straight into
#! whatever takes it and nothing throwaway is built on the way.
#!
#! A type that must not be copied simply does not implement this trait, which is
#! why the function gives the copy rather than an error: the type system says
#! what cannot be copied, and running out of memory stops the program, as
#! `acquire` does.
pub trait copyable {
#! Builds a copy of a value, which owns copies of whatever the value owns.
#! @param other The value to copy.
#! @returns The copy.
def copy_of(other: &self): self;
}
#! Shortcut function for copying a value, giving the copy back.
#! @tparam T The type of the value.
#! @param src The source object to copy.
#! @returns The copy, which whatever takes it owns. A type that is `copyable` is
#! copied by its `copy_of`, and one that closes nothing byte for byte.
#! @remarks The copy is given back rather than written into a destination that
#! already exists, so that returning it writes straight into whatever
#! takes it. A type that closes, or holds what does, and is not
#! `copyable` is an error where it is copied, since its bytes would
#! share what it closes, and both copies would close it.
pub def copy<T>(src: &T): T {
if const T is copyable {
return T::copy_of(src);
}
if const T is closeable {
compile_error("`copy` copies a value that closes, or holds one that does, only through `copyable`, which this type does not implement.");
}
return src;
}
#! Defines that a type or generic must conform to being moveable, where the data
#! of one object is handed to a new one and the source left empty.
#!
#! The function is static, and gives the moved value back rather than filling a
#! destination that already exists, so that returning it writes straight into
#! whatever takes it and nothing throwaway is built on the way.
#!
#! The source is `&mut` because a move empties it, which is the whole difference
#! between this and `copyable`. What it leaves is valid but empty rather than
#! dead, so closing it afterwards is harmless.
pub trait moveable {
#! Builds a value from what another holds, leaving the other empty.
#! @param other The value to move from, which is left empty but valid.
#! @returns The new value, holding what the other held.
def move_of(other: &mut self): self;
}
#! Moves a value out of where it stands, leaving it empty.
#! @tparam T The type of the value.
#! @param src The value to move from, which holds nothing after.
#! @returns The moved value, which whatever takes it owns. A type that is
#! `moveable` is moved by its `move_of`, and any other byte for byte.
#! @remarks The source is left valid but empty rather than dead, so closing it
#! afterwards is harmless: one that closes, or holds what does, is
#! zeroed. Plain data, which nothing closes, is left as it was.
pub def move<T>(src: &mut T): T {
if const T is moveable {
return T::move_of(src);
}
# Plain data, which nothing closes, is copied as it is.
if const !(T is closeable) {
return src;
}
# Fallback: read the value out byte for byte, then leave the source zeroed, so it
# is not closed twice: by a store of each word where it is one to four words, as
# most that close are, and by `memset` otherwise.
val made := bits_of(src as *T);
val words := src as *mut u64;
if const size_of::<T>() == 8 {
words[0] = 0;
} else if const size_of::<T>() == 16 {
words[0] = 0;
words[1] = 0;
} else if const size_of::<T>() == 24 {
words[0] = 0;
words[1] = 0;
words[2] = 0;
} else if const size_of::<T>() == 32 {
words[0] = 0;
words[1] = 0;
words[2] = 0;
words[3] = 0;
} else {
memset(src as *mut u8, 0, size_of::<T>());
}
return made;
}
# The value a pointer points at, byte for byte. A `return` hands over what a pointer
# points at without emptying it, so the caller decides what is left there.
def bits_of<T>(from: *T): T {
return from[0];
}