Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value size reduction - part one #236

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion core/memory/src/rc/ptr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::{fmt, ops::Deref, rc::Rc};
use std::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
ops::Deref,
rc::Rc,
};

use super::Address;

Expand Down Expand Up @@ -27,12 +33,31 @@ impl<T: ?Sized> Ptr<T> {
}
}

impl<T: Clone> Ptr<T> {
/// Makes a mutable reference into the owned `T`
///
/// If the pointer has the only reference to the value, then the reference will be returned.
/// Otherwise a clone of the value will be made to ensure uniqueness before returning the
/// reference.
///
/// See also: [std::rc::Rc::make_mut]
pub fn make_mut(this: &mut Self) -> &mut T {
Rc::make_mut(&mut this.0)
}
}

impl<T> From<T> for Ptr<T> {
fn from(value: T) -> Self {
Self::new(value)
}
}

impl<T: ?Sized> From<Box<T>> for Ptr<T> {
fn from(boxed: Box<T>) -> Self {
Self(boxed.into())
}
}

impl<T: ?Sized> From<Rc<T>> for Ptr<T> {
fn from(inner: Rc<T>) -> Self {
Self(inner)
Expand Down Expand Up @@ -87,8 +112,29 @@ impl<T: ?Sized + PartialEq> PartialEq for Ptr<T> {
}
}

impl<T: ?Sized + Eq> Eq for Ptr<T> {}

impl<T: ?Sized + fmt::Display> fmt::Display for Ptr<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl<T: ?Sized + Hash> Hash for Ptr<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}

impl<T: ?Sized + Ord> Ord for Ptr<T> {
#[inline]
fn cmp(&self, other: &Ptr<T>) -> Ordering {
self.0.cmp(&other.0)
}
}

impl<T: ?Sized + PartialOrd> PartialOrd for Ptr<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}
30 changes: 15 additions & 15 deletions core/runtime/src/core_lib/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ pub fn make_module() -> ValueMap {
});

result.add_fn("end", |vm, args| match vm.get_args(args) {
[Range(r)] => Ok(r.end.map_or(Null, |(end, _inclusive)| end.into())),
[Range(r)] => Ok(r.end().map_or(Null, |(end, _inclusive)| end.into())),
unexpected => type_error_with_slice("a Range as argument", unexpected),
});

result.add_fn("expanded", |vm, args| match vm.get_args(args) {
[Range(r), Number(n)] => match (r.start, r.end) {
[Range(r), Number(n)] => match (r.start(), r.end()) {
(Some(start), Some((end, inclusive))) => {
let n = isize::from(n);
let n = i64::from(n);
let result = if r.is_ascending() {
IntRange::with_bounds(start - n, end + n, inclusive)
IntRange::bounded(start - n, end + n, inclusive)
} else {
IntRange::with_bounds(start + n, end - n, inclusive)
IntRange::bounded(start + n, end - n, inclusive)
};
Ok(result.into())
}
Expand All @@ -43,12 +43,12 @@ pub fn make_module() -> ValueMap {
});

result.add_fn("intersection", |vm, args| match vm.get_args(args) {
[Range(a), Range(b)] => Ok(a.intersection(*b).map_or(Null, |result| result.into())),
[Range(a), Range(b)] => Ok(a.intersection(b).map_or(Null, |result| result.into())),
unexpected => type_error_with_slice("two Ranges", unexpected),
});

result.add_fn("is_inclusive", |vm, args| match vm.get_args(args) {
[Range(r)] => Ok(r.end.map_or(false, |(_end, inclusive)| inclusive).into()),
[Range(r)] => Ok(r.end().map_or(false, |(_end, inclusive)| inclusive).into()),
unexpected => type_error_with_slice("a Range as argument", unexpected),
});

Expand All @@ -61,32 +61,32 @@ pub fn make_module() -> ValueMap {
});

result.add_fn("start", |vm, args| match vm.get_args(args) {
[Range(r)] => Ok(r.start.map_or(Null, |start| start.into())),
[Range(r)] => Ok(r.start().map_or(Null, Value::from)),
unexpected => type_error_with_slice("a Range as argument", unexpected),
});

result.add_fn("union", |vm, args| match vm.get_args(args) {
[Range(r), Number(n)] => {
let n = isize::from(n);
match (r.start, r.end) {
let n = i64::from(n);
match (r.start(), r.end()) {
(Some(start), Some((end, inclusive))) => {
let result = if start <= end {
IntRange::with_bounds(start.min(n), end.max(n + 1), inclusive)
IntRange::bounded(start.min(n), end.max(n + 1), inclusive)
} else {
IntRange::with_bounds(start.max(n), end.min(n - 1), inclusive)
IntRange::bounded(start.max(n), end.min(n - 1), inclusive)
};
Ok(result.into())
}
_ => runtime_error!("range.union can't be used with '{r}'"),
}
}
[Range(a), Range(b)] => match (a.start, a.end) {
[Range(a), Range(b)] => match (a.start(), a.end()) {
(Some(start), Some((end, inclusive))) => {
let r_b = b.as_sorted_range();
let result = if start <= end {
IntRange::with_bounds(start.min(r_b.start), end.max(r_b.end), inclusive)
IntRange::bounded(start.min(r_b.start), end.max(r_b.end), inclusive)
} else {
IntRange::with_bounds(start.max(r_b.end - 1), end.min(r_b.start), inclusive)
IntRange::bounded(start.max(r_b.end - 1), end.min(r_b.start), inclusive)
};
Ok(result.into())
}
Expand Down
Loading
Loading