//! Shared dependency with private writable state.

/// Value read by dependent packages.
export unsafe static value: i32 = 7;

/// Large private allocation before the final zero-initialized scalar.
unsafe static padding: [u8; 8192] = [0; 8192];
/// Zero-initialized value at a large package offset.
unsafe static tail: i32 = 0;

/// Read this domain's value.
export fn read() -> i32 {
    unsafe {
        set padding[8191] = 0;
        return value + tail;
    }
}

/// Value with a public nominal identity and one trait implementation.
export record Number: Copy {
    /// Value read through the trait.
    value: i32,
    /// Second word that requires a caller-provided return buffer.
    extra: u64,
}

/// Read access to a value.
export trait Reading {
    /// Read the receiver's value.
    fn (&Reading) get() -> i32;
}

/// Reading access to a Number.
instance Reading for Number {
    /// Read the receiver's value.
    fn (self: &Number) get() -> i32 {
        return self.value;
    }
}

/// Preserve the input storage region in the result.
export fn borrow(value: &'a Number) -> &'a Number {
    return value;
}

/// Return a record through the caller's output buffer.
export fn copied(value: &Number) -> Number {
    return *value;
}

/// Dispatch through a borrowed trait supplied by the caller.
export fn reading(value: &opaque Reading) -> i32 {
    return value.get();
}

/// A borrowed value returned through the caller's storage.
export record Reference['r]: Copy {
    /// Storage borrowed from the input value.
    value: &'r Number,
}

/// Transfer the input borrow into the caller's result buffer.
export fn referenced(value: &'r Number) -> Reference['r] {
    return Reference { value };
}
