Skip to content

Commit

Permalink
Some fixes in arch
Browse files Browse the repository at this point in the history
  • Loading branch information
uselessgoddess committed Jul 23, 2022
1 parent 7dbbacc commit a2cadfa
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 46 deletions.
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Data for linksplatform

[dependencies]
funty = "2.0.0"
thiserror = "1.0.31"

[dev-dependencies]
quickcheck = "1.0.3"
Expand Down
18 changes: 6 additions & 12 deletions rust/src/converters.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
use crate::Hybrid;
use crate::LinkType;
use funty::Integral;
use std::ops::Sub;

#[derive(Default)]
pub struct AddrToRaw;

impl AddrToRaw {
pub fn new() -> Self {
Self
}

pub fn convert<T: LinkType>(&self, source: T) -> T {
Hybrid::new(source).as_inner()
pub const fn convert<T: LinkType + ~const Integral + ~const Sub>(&self, source: T) -> T {
Hybrid::external(source).as_inner()
}
}

#[derive(Default)]
pub struct RawToAddr;

impl RawToAddr {
pub fn new() -> Self {
Self
}

pub fn convert<T: LinkType>(&self, source: T) -> T {
Hybrid::new(source).abs()
pub const fn convert<T: LinkType + ~const Integral + ~const Sub>(&self, source: T) -> T {
Hybrid::external(source).abs()
}
}
43 changes: 31 additions & 12 deletions rust/src/hybrid.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::LinkType;
use std::ops::Div;
use funty::Integral;
use std::ops::{Div, Sub};

#[derive(Debug, Clone, Copy, Hash, PartialOrd, PartialEq, Ord, Eq)]
pub struct Hybrid<T> {
value: T,
}

impl<T: LinkType> Hybrid<T> {
pub fn new(value: T) -> Self {
pub const fn new(value: T) -> Self {
Self::internal(value)
}

Expand All @@ -18,37 +19,55 @@ impl<T: LinkType> Hybrid<T> {
T::MAX / T::funty(2)
}

pub fn external(value: T) -> Self {
pub(crate) const fn external(value: T) -> Self
where
T: ~const Integral + ~const Sub,
{
Self {
value: Self::extend_value(value),
}
}

pub fn internal(value: T) -> Self {
pub(crate) const fn internal(value: T) -> Self {
Self { value }
}

fn extend_value(value: T) -> T {
const fn extend_value(value: T) -> T
where
T: ~const Integral + ~const Sub,
{
(T::MAX - value).wrapping_add(T::funty(1))
}

pub fn is_zero(&self) -> bool {
self.value == T::default()
pub const fn is_zero(&self) -> bool
where
T: ~const Default + ~const PartialEq,
{
self.value == T::funty(0)
}

pub fn is_internal(&self) -> bool {
pub const fn is_internal(&self) -> bool
where
T: ~const Div + ~const PartialOrd,
{
self.value < Self::half() // || self.value == T::default()
}

pub fn is_external(&self) -> bool {
!self.is_internal() || self.value == T::default()
pub const fn is_external(&self) -> bool
where
T: ~const Div + ~const PartialOrd + ~const PartialEq,
{
!self.is_internal() || self.value == T::funty(0)
}

pub fn abs(&self) -> T {
pub const fn abs(&self) -> T
where
T: ~const Integral,
{
self.value.wrapping_add(T::funty(1)).wrapping_add(T::MAX)
}

pub fn as_inner(&self) -> T {
pub const fn as_inner(&self) -> T {
self.value
}
}
5 changes: 4 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#![feature(associated_type_bounds)]
#![feature(try_trait_v2)]
#![feature(type_alias_impl_trait)]
#![feature(const_refs_to_cell)]
#![feature(const_result_drop)]
#![feature(const_trait_impl)]
#![feature(const_convert)]
#![feature(const_result_drop)]
#![feature(const_deref)]
#![feature(backtrace)]

mod constants;
mod converters;
Expand Down
45 changes: 32 additions & 13 deletions rust/src/links.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
use crate::{Flow, LinkType, LinksConstants};
use std::error::Error;
use std::{borrow::Cow, error, io};

#[derive(thiserror::Error, Debug)]
pub enum Error<'a, T: LinkType> {
#[error("link {0} does not exist.")]
NotExists(T),

#[error("link {0:?} has dependencies")]
HasUsages(Vec<Cow<'a, [T]>>),

#[error("link {0} already exists")]
AlreadyExists(Cow<'a, T>),

#[error("limit for the number of links in the storage has been reached: {0}")]
LimitReached(T),

#[error("unable to allocate memory for links storage: `{0}`")]
AllocFailed(
#[from]
#[backtrace]
io::Error,
),

#[error("other internal error: `{0}`")]
Other(#[from] Box<dyn error::Error + Sync + Send>),
}

pub type ReadHandler<'a, T> = &'a mut dyn FnMut(&[T]) -> Flow;

Expand All @@ -10,24 +35,18 @@ pub trait Links<T: LinkType> {

fn count_links(&self, query: &[T]) -> T;

fn create_links(
&mut self,
query: &[T],
handler: WriteHandler<T>,
) -> Result<Flow, Box<dyn Error>>;
fn create_links(&mut self, query: &[T], handler: WriteHandler<T>)
-> Result<Flow, Error<'_, T>>;

fn each_links(&self, query: &[T], handler: ReadHandler<T>) -> Result<Flow, Box<dyn Error>>;
fn each_links(&self, query: &[T], handler: ReadHandler<T>) -> Result<Flow, Error<'_, T>>;

fn update_links(
&mut self,
query: &[T],
replacement: &[T],
handler: WriteHandler<T>,
) -> Result<Flow, Box<dyn Error>>;
) -> Result<Flow, Error<'_, T>>;

fn delete_links(
&mut self,
query: &[T],
handler: WriteHandler<T>,
) -> Result<Flow, Box<dyn Error>>;
fn delete_links(&mut self, query: &[T], handler: WriteHandler<T>)
-> Result<Flow, Error<'_, T>>;
}
8 changes: 4 additions & 4 deletions rust/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pub struct Point<T> {
}

impl<T: PartialEq + Clone> Point<T> {
pub fn new(index: T, size: usize) -> Self {
pub const fn new(index: T, size: usize) -> Self {
Self { index, size }
}

pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.size
}

Expand All @@ -38,7 +38,7 @@ impl<T: PartialEq + Clone> Point<T> {
link.iter().skip(1).any(|b| b == a)
}

pub fn get(&self, index: usize) -> Option<&T> {
pub const fn get(&self, index: usize) -> Option<&T> {
if index < self.len() {
Some(&self.index)
} else {
Expand Down
10 changes: 7 additions & 3 deletions rust/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::marker::Destruct;
use std::ops::Index;
use std::slice::SliceIndex;

Expand All @@ -13,15 +14,18 @@ impl<'a, T: Clone> Query<'a, T> {
Query(beef.into())
}

pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.0.is_empty()
}

pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.0.len()
}

pub fn into_inner(self) -> Cow<'a, [T]> {
pub const fn into_inner(self) -> Cow<'a, [T]>
where
Self: ~const Destruct,
{
self.0
}

Expand Down
4 changes: 3 additions & 1 deletion rust/tests/hybrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ use quickcheck_macros::quickcheck;

#[quickcheck]
fn basic(orig: usize) -> bool {
AddrToRaw.convert(RawToAddr.convert(orig)) == orig && Hybrid::new(orig).abs() == orig
const HYBR: Hybrid<usize> = Hybrid::<usize>::external(usize::MAX / 2 + 123);

RawToAddr.convert(AddrToRaw.convert(orig)) == orig && Hybrid::new(orig).abs() == orig
}

0 comments on commit a2cadfa

Please sign in to comment.