From 9747d94ccc9caab86df2882fb7859e186f91fba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charlotte=20=F0=9F=A6=9D=20Delenk?= Date: Fri, 8 Jul 2022 20:59:14 +0100 Subject: [PATCH] add tinyptr-alloc --- Cargo.lock | 7 ++++ Cargo.nix | 12 +++++++ Cargo.toml | 3 +- flake.nix | 49 ++++++++++++++++----------- lib/tinyptr-alloc/Cargo.toml | 9 +++++ lib/tinyptr-alloc/src/lib.rs | 10 ++++++ lib/tinyptr/src/lib.rs | 2 ++ lib/tinyptr/src/tiny_ref/const_ref.rs | 31 +++++++++++++++++ lib/tinyptr/src/tiny_ref/mod.rs | 2 ++ 9 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 lib/tinyptr-alloc/Cargo.toml create mode 100644 lib/tinyptr-alloc/src/lib.rs create mode 100644 lib/tinyptr/src/tiny_ref/const_ref.rs create mode 100644 lib/tinyptr/src/tiny_ref/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 68c25ee..1f0d885 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -569,6 +569,13 @@ dependencies = [ name = "tinyptr" version = "0.1.0" +[[package]] +name = "tinyptr-alloc" +version = "0.1.0" +dependencies = [ + "tinyptr", +] + [[package]] name = "unicode-ident" version = "1.0.1" diff --git a/Cargo.nix b/Cargo.nix index 4612c16..d5e5525 100644 --- a/Cargo.nix +++ b/Cargo.nix @@ -5,6 +5,7 @@ args@{ release ? true, rootFeatures ? [ "tinyptr/default" + "tinyptr-alloc/default" "rkbfirm/default" ], rustPackages, @@ -43,6 +44,7 @@ in cargo2nixVersion = "0.11.0"; workspace = { tinyptr = rustPackages.unknown.tinyptr."0.1.0"; + tinyptr-alloc = rustPackages.unknown.tinyptr-alloc."0.1.0"; rkbfirm = rustPackages.unknown.rkbfirm."0.1.0"; }; "registry+https://github.com/rust-lang/crates.io-index".aho-corasick."0.7.18" = overridableMkRustCrate (profileName: rec { @@ -810,6 +812,16 @@ in src = fetchCrateLocal (workspaceSrc + "/lib/tinyptr"); }); + "unknown".tinyptr-alloc."0.1.0" = overridableMkRustCrate (profileName: rec { + name = "tinyptr-alloc"; + version = "0.1.0"; + registry = "unknown"; + src = fetchCrateLocal (workspaceSrc + "/lib/tinyptr-alloc"); + dependencies = { + tinyptr = rustPackages."unknown".tinyptr."0.1.0" { inherit profileName; }; + }; + }); + "registry+https://github.com/rust-lang/crates.io-index".unicode-ident."1.0.1" = overridableMkRustCrate (profileName: rec { name = "unicode-ident"; version = "1.0.1"; diff --git a/Cargo.toml b/Cargo.toml index 0031c77..cd26830 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,8 @@ edition = "2021" [workspace] members = [ - "lib/tinyptr" + "lib/tinyptr", + "lib/tinyptr-alloc" ] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/flake.nix b/flake.nix index 46b6721..aac8895 100644 --- a/flake.nix +++ b/flake.nix @@ -19,8 +19,15 @@ }; }; - outputs = { self, nixpkgs, flake-utils, rust-overlay, cargo2nix, ... } @ inputs: flake-utils.lib.eachSystem [ "x86_64-linux" ] (system: - let + outputs = { + self, + nixpkgs, + flake-utils, + rust-overlay, + cargo2nix, + ... + } @ inputs: + flake-utils.lib.eachSystem ["x86_64-linux"] (system: let overlays = [ cargo2nix.overlays.default (import rust-overlay) @@ -35,27 +42,27 @@ target = "thumbv6m-none-eabi"; packageOverrides = pkgs: pkgs.rustBuilder.overrides.all; }; - in - rec { - devShells.default = with pkgs; mkShell { - buildInputs = [ - (rust-bin.nightly.latest.default.override { - extensions = [ "rust-src" ]; - targets = ["thumbv6m-none-eabi"]; - }) - cargo2nix.packages.${system}.cargo2nix - elf2uf2-rs - cargo-embed - llvmPackages_latest.bintools - ]; - }; - packages = rec { + in rec { + devShells.default = with pkgs; + mkShell { + buildInputs = [ + (rust-bin.nightly.latest.default.override { + extensions = ["rust-src"]; + targets = ["thumbv6m-none-eabi"]; + }) + cargo2nix.packages.${system}.cargo2nix + elf2uf2-rs + cargo-embed + llvmPackages_latest.bintools + ]; + }; + packages = rec { rkbfirm-source = pkgs.releaseTools.sourceTarball { name = "rkbfirm-source"; src = self; officialRelease = true; version = self.lastModifiedDate; - nativeBuildInputs = [ pkgs.zstd ]; + nativeBuildInputs = [pkgs.zstd]; distPhase = '' releaseName=rkb1-src-$version mkdir -p $out/tarballs @@ -64,9 +71,11 @@ (cd .. && tar -cf- $releaseName | zstd --ultra -22 > $out/tarballs/$releaseName.tar.zst) || false ''; }; - rkbfirm-crate = (rustPkgs.workspace.rkbfirm { }).overrideAttrs(old: { + rkbfirm-crate = (rustPkgs.workspace.rkbfirm {}).overrideAttrs (old: { configureCargo = "true"; }); + tinyptr-crate = rustPkgs.workspace.tinyptr {}; + tinyptr-alloc-crate = rustPkgs.workspace.tinyptr-alloc {}; rkbfirm = pkgs.stdenvNoCC.mkDerivation { pname = "rkbfirm"; src = self; @@ -93,10 +102,12 @@ }; default = rkbfirm; devShell = devShells.default; + inherit formatter; }; nixosModules.default = import ./nixos { inherit inputs system; }; hydraJobs = packages; + formatter = pkgs.alejandra; }); } diff --git a/lib/tinyptr-alloc/Cargo.toml b/lib/tinyptr-alloc/Cargo.toml new file mode 100644 index 0000000..a5d9370 --- /dev/null +++ b/lib/tinyptr-alloc/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "tinyptr-alloc" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tinyptr = { path = "../tinyptr" } diff --git a/lib/tinyptr-alloc/src/lib.rs b/lib/tinyptr-alloc/src/lib.rs new file mode 100644 index 0000000..b7c2b75 --- /dev/null +++ b/lib/tinyptr-alloc/src/lib.rs @@ -0,0 +1,10 @@ +#![no_std] + +use tinyptr::ptr::MutPtr; + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct ListNode { + pub next: MutPtr, + pub size: u16 +} + diff --git a/lib/tinyptr/src/lib.rs b/lib/tinyptr/src/lib.rs index eb3452d..7a21e6b 100644 --- a/lib/tinyptr/src/lib.rs +++ b/lib/tinyptr/src/lib.rs @@ -19,6 +19,8 @@ use core::hash::Hash; pub mod ptr; +mod tiny_ref; +pub use tiny_ref::*; /// Trait that defines valid destination types for a pointer. pub trait Pointable { diff --git a/lib/tinyptr/src/tiny_ref/const_ref.rs b/lib/tinyptr/src/tiny_ref/const_ref.rs new file mode 100644 index 0000000..db5ff22 --- /dev/null +++ b/lib/tinyptr/src/tiny_ref/const_ref.rs @@ -0,0 +1,31 @@ +use core::{marker::PhantomData, ops::Deref, borrow::Borrow}; + +use crate::{Pointable, ptr::NonNull}; + +/// Constant Tiny Reference +#[repr(transparent)] +pub struct Ref<'a, T: Pointable + ?Sized, const BASE: usize> { + pub(crate) ptr: NonNull, + pub(crate) _marker: PhantomData<&'a T> +} + +impl Copy for Ref<'_, T, BASE> {} +impl Clone for Ref<'_, T, BASE> { + fn clone(&self) -> Self { + *self + } +} +impl Deref for Ref<'_, T, BASE> { + type Target = T; + fn deref(&self) -> &T { + // SAFETY: Reference must be valid to be constructed + unsafe { + &*(*self).ptr.as_ptr().wide() + } + } +} +impl Borrow for Ref<'_, T, BASE> { + fn borrow(&self) -> &T { + &*self + } +} diff --git a/lib/tinyptr/src/tiny_ref/mod.rs b/lib/tinyptr/src/tiny_ref/mod.rs new file mode 100644 index 0000000..907f62c --- /dev/null +++ b/lib/tinyptr/src/tiny_ref/mod.rs @@ -0,0 +1,2 @@ +mod const_ref; +pub use const_ref::*;