Skip to content

Commit

Permalink
fix: remove use of unstable once_cell_try feature
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Dec 7, 2024
1 parent b54d658 commit 5e4947a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 54 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
rust: [stable, nightly]
release: [false, true]
exclude:
- os: macos-latest
rust: nightly
- os: windows-latest
rust: nightly

runs-on: ${{ matrix.os }}

Expand Down Expand Up @@ -43,8 +47,4 @@ jobs:

- name: cargo build
run: |
if [ "${{ matrix.release }}" = "true" ]; then
cargo build --workspace --release --locked --all-targets
else
cargo build --workspace --locked --all-targets
fi
cargo build --workspace --release --locked --all-targets
83 changes: 35 additions & 48 deletions crates/source/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#![feature(once_cell_try)]

use std::borrow::Cow;
use std::cell::OnceCell;
use std::cmp::Ordering;
use std::ops::Range;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;

use codespan_reporting::files::line_starts;
use codespan_reporting::files::Error;
Expand Down Expand Up @@ -48,7 +44,7 @@ struct SourceEntry {
/// The path to the source.
path: Option<PathBuf>,
/// The content of the source.
content: Mutex<OnceCell<(StringIdentifier, usize, Vec<usize>)>>,
content: Option<(StringIdentifier, usize, Vec<usize>)>,
}

/// A manager for sources, which stores sources and provides methods to insert and retrieve them.
Expand Down Expand Up @@ -132,7 +128,7 @@ impl SourceManager {
return source_id;
}

self.sources.insert(source_id, SourceEntry { name, path: Some(path), content: Mutex::new(OnceCell::new()) });
self.sources.insert(source_id, SourceEntry { name, path: Some(path), content: None });

source_id
}
Expand All @@ -158,10 +154,7 @@ impl SourceManager {
let size = content.len();
let content = self.interner.intern(content);

self.sources.insert(
source_id,
SourceEntry { name, path: None, content: Mutex::new(OnceCell::from((content, size, lines))) },
);
self.sources.insert(source_id, SourceEntry { name, path: None, content: Some((content, size, lines)) });

source_id
}
Expand Down Expand Up @@ -204,20 +197,21 @@ impl SourceManager {
///
/// The source with the given identifier, or an error if the source does not exist, or could not be loaded.
pub fn load(&self, source_id: SourceIdentifier) -> Result<Source, SourceError> {
let Some(entry) = self.sources.get(&source_id) else {
let Some(mut entry) = self.sources.get_mut(&source_id) else {
return Err(SourceError::UnavailableSource(source_id));
};

let content_cell = entry.value().content.lock().expect("failed to aquire lock on entry content");

let content = content_cell
.get_or_try_init(|| {
let path = entry
.path
.as_ref()
.expect("source entry must contain either content or path");

std::fs::read(path)
match &entry.content {
Some((content, size, lines)) => Ok(Source {
identifier: source_id,
path: entry.path.clone(),
content: *content,
size: *size,
lines: lines.clone(),
}),
None => {
let path = entry.path.clone().expect("source entry must contain either content or path");
let content = std::fs::read(&path)
.map(|bytes| match String::from_utf8_lossy(&bytes) {
Cow::Borrowed(str) => str.to_string(),
Cow::Owned(string) => {
Expand All @@ -228,25 +222,20 @@ impl SourceManager {

string
}
})
.map(|content| {
let lines = line_starts(&content).collect();
let size = content.len();
let content = self.interner.intern(content);

(content, size, lines)
})
});

match content {
Ok((content, size, lines)) => Ok(Source {
identifier: source_id,
path: entry.path.clone(),
content: *content,
size: *size,
lines: lines.clone(),
}),
Err(err) => Err(SourceError::IOError(err)),
})?;

let (_, v) = entry.pair_mut();

let lines: Vec<_> = line_starts(&content).collect();
let size = content.len();
let content = self.interner.intern(content);

let source = Source { identifier: source_id, path: Some(path), content, size, lines: lines.clone() };

v.content = Some((content, size, lines));

Ok(source)
}
}
}

Expand All @@ -259,14 +248,13 @@ impl SourceManager {
let content = self.interner.intern(content);

let (_, v) = entry.pair_mut();
if let Some((old_content, _, _)) = v.content.lock().expect("failed to aquire lock on entry content").get() {
if let Some((old_content, _, _)) = v.content.as_mut() {
if *old_content == content {
return Ok(());
}
}

v.content = Mutex::new(OnceCell::from((content, size, lines)));

v.content = Some((content, size, lines));
if let Some(path) = entry.value().path.as_ref() {
std::fs::write(path, self.interner.lookup(&content)).map_err(SourceError::IOError)?;
}
Expand All @@ -289,11 +277,10 @@ impl SourceManager {
.get(&source_id)
.map(|entry| {
let entry = entry.value();

let content_cell = entry.content.lock().expect("failed to aquire lock on entry content");

let (content, size, lines) =
content_cell.get().expect("content must be initialized when source entry is present in the map");
let (content, size, lines) = entry
.content
.as_ref()
.expect("content must be initialized when source entry is present in the map");

Source {
identifier: source_id,
Expand Down

1 comment on commit 5e4947a

@azjezz
Copy link
Member Author

@azjezz azjezz commented on 5e4947a Dec 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea why i used OnceCell to begin with here..

Please sign in to comment.