Skip to content
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
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true,
"editor.rulers": [
150
]
},
"rust-analyzer.cargo.cfgs": [
"!miri"
],
"rust-analyzer.check.command": "clippy"
}
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
resolver = "2"
# Split to default members without tests and examples.
# Used when executing cargo from project root.
default-members = ["src/log"]
default-members = [
"src/containers",
"src/log"
]
# Include tests and examples as a member for IDE support and Bazel builds.
members = ["src/log"]
members = [
"src/containers",
"src/log"
]


[workspace.package]
Expand Down
4 changes: 3 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
# check configuration fields here: https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=


tab_spaces = 4
match_block_trailing_comma = true
max_width = 150
tab_spaces = 4
use_field_init_shorthand = true
30 changes: 30 additions & 0 deletions src/containers/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# *******************************************************************************
# Copyright (c) 2025 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

rust_library(
name = "containers",
srcs = glob(["**/*.rs"]),
edition = "2024",
visibility = ["//visibility:public"],
)

rust_test(
name = "tests",
crate = "containers",
tags = [
"unit_tests",
"ut",
],
)
23 changes: 23 additions & 0 deletions src/containers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# *******************************************************************************
# Copyright (c) 2025 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************

[package]
name = "containers"
description = "Fixed-capacity and inline-storage containers"
version = "0.1.0"
authors = ["Contributors to the Eclipse Foundation"]
edition = "2024"
license-file = "../../LICENSE.md"

[lib]
path = "lib.rs"
16 changes: 16 additions & 0 deletions src/containers/fixed_capacity/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// *******************************************************************************
// Copyright (c) 2025 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: Apache-2.0
// *******************************************************************************

mod vec;

pub use self::vec::FixedCapacityVec;
136 changes: 136 additions & 0 deletions src/containers/fixed_capacity/vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// *******************************************************************************
// Copyright (c) 2025 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: Apache-2.0
// *******************************************************************************

use core::fmt;
use core::ops;

use crate::generic::vec::GenericVec;
use crate::storage::Heap;

/// A fixed-capacity vector.
///
/// The vector can hold between 0 and `CAPACITY` elements, and behaves similarly to Rust's `Vec`,
/// except that it allocates memory immediately on construction, and can't shrink or grow.
pub struct FixedCapacityVec<T> {
inner: GenericVec<T, Heap<T>>,
}

impl<T> FixedCapacityVec<T> {
/// Creates an empty vector and allocates memory for up to `capacity` elements, where `capacity <= u32::MAX`.
///
/// # Panics
///
/// - Panics if `capacity > u32::MAX`.
/// - Panics if the memory allocation fails.
#[must_use]
pub fn new(capacity: usize) -> Self {
assert!(capacity <= u32::MAX as usize, "FixedCapacityVec can hold at most u32::MAX elements");
Self {
inner: GenericVec::new(capacity as u32),
}
}
}

impl<T> Drop for FixedCapacityVec<T> {
fn drop(&mut self) {
self.inner.clear();
}
}

impl<T> ops::Deref for FixedCapacityVec<T> {
type Target = GenericVec<T, Heap<T>>;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl<T> ops::DerefMut for FixedCapacityVec<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}

impl<T: fmt::Debug> fmt::Debug for FixedCapacityVec<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_slice(), f)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn push_and_pop() {
fn run_test(n: usize) {
let mut vector = FixedCapacityVec::<i64>::new(n);
let mut control = vec![];

let result = vector.pop();
assert_eq!(result, None);

for i in 0..n {
let value = i as i64 * 123 + 456;
let result = vector.push(value);
assert_eq!(*result.unwrap(), value);
control.push(value);
assert_eq!(vector.as_slice(), control.as_slice());
}

let result = vector.push(123456);
assert!(result.is_err());

for _ in 0..n {
let expected = control.pop().unwrap();
let actual = vector.pop();
assert_eq!(actual, Some(expected));
}

let result = vector.pop();
assert_eq!(result, None);
}

for i in 0..6 {
run_test(i);
}
}

#[test]
fn is_full_and_is_empty() {
fn run_test(n: usize) {
let mut vector = FixedCapacityVec::<i64>::new(n);
assert!(vector.is_empty());

for i in 0..n {
assert!(!vector.is_full());
vector.push(i as i64 * 123 + 456).unwrap();
assert!(!vector.is_empty());
}

assert!(vector.is_full());

for _ in 0..n {
assert!(!vector.is_empty());
vector.pop();
assert!(!vector.is_full());
}

assert!(vector.is_empty());
}

for i in 0..6 {
run_test(i);
}
}
}
14 changes: 14 additions & 0 deletions src/containers/generic/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// *******************************************************************************
// Copyright (c) 2025 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: Apache-2.0
// *******************************************************************************

pub(crate) mod vec;
Loading
Loading