Skip to content

Commit

Permalink
Remove 'std' feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Aug 22, 2021
1 parent a9e01cd commit 3e2ea12
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 39 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ categories = ["data-structures", "no-std"]
readme = "README.md"
include = ["src/**/*", "LICENSE", "README.md"]

[features]
default = []
std = []

[dependencies]
arrayvec = { version = "0.7", default-features = false }
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ Servo's style system.
insertion, and `O(n)` lookup. It does not require an allocator and can be
used in `no_std` crates. It is implemented in 100% safe Rust.

## Cargo Features

By default, this crate won't need the standard library. However, if the `std` cargo feature is enabled, `insert()` will
return a replaced value which allows to reuse memory it may have contained.

* [Documentation](https://docs.rs/uluru)
* [crates.io](https://crates.io/crates/uluru)
* [Release notes](https://github.com/servo/uluru/releases)
34 changes: 4 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]
#![deny(unsafe_code)]

//! A simple, fast, least-recently-used (LRU) cache.
Expand All @@ -13,7 +13,7 @@
//! See the [`LRUCache`](LRUCache) docs for details.
use arrayvec::ArrayVec;
use core::fmt;
use core::{fmt, mem::replace};

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -95,8 +95,7 @@ impl<T, const N: usize> LRUCache<T, N> {
/// Insert a given key in the cache.
///
/// This item becomes the front (most-recently-used) item in the cache. If the cache is full,
/// the back (least-recently-used) item will be removed.
#[cfg(feature = "std")]
/// the back (least-recently-used) item will be removed and returned.
pub fn insert(&mut self, val: T) -> Option<T> {
let entry = Entry {
val,
Expand All @@ -107,7 +106,7 @@ impl<T, const N: usize> LRUCache<T, N> {
// If the cache is full, replace the oldest entry. Otherwise, add an entry.
let (new_head, previous_entry) = if self.entries.len() == self.entries.capacity() {
let i = self.pop_back();
let previous_entry = std::mem::replace(&mut self.entries[i as usize], entry);
let previous_entry = replace(&mut self.entries[i as usize], entry);
(i, Some(previous_entry.val))
} else {
self.entries.push(entry);
Expand All @@ -118,31 +117,6 @@ impl<T, const N: usize> LRUCache<T, N> {
previous_entry
}

/// Insert a given key in the cache.
///
/// This item becomes the front (most-recently-used) item in the cache. If the cache is full,
/// the back (least-recently-used) item will be removed.
#[cfg(not(feature = "std"))]
pub fn insert(&mut self, val: T) {
let entry = Entry {
val,
prev: 0,
next: 0,
};

// If the cache is full, replace the oldest entry. Otherwise, add an entry.
let new_head = if self.entries.len() == self.entries.capacity() {
let i = self.pop_back();
self.entries[i as usize] = entry;
i
} else {
self.entries.push(entry);
self.entries.len() as u16 - 1
};

self.push_front(new_head);
}

/// Returns the first item in the cache that matches the given predicate.
/// Touches the result (makes it most-recently-used) on a hit.
pub fn find<F>(&mut self, pred: F) -> Option<&mut T>
Expand Down

0 comments on commit 3e2ea12

Please sign in to comment.