Skip to content

Commit

Permalink
does this mean I actually understand rust lifetimes now?
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 1, 2023
1 parent a360f75 commit 222a608
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 36 deletions.
39 changes: 31 additions & 8 deletions fastn-core/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,24 +593,47 @@ async fn handle_file_(
return Ok(());
}

let mut req_config =
fastn_core::RequestConfig::new(&config, &fastn_core::http::Request::default());
let req = fastn_core::http::Request::default();
let mut req_config = fastn_core::RequestConfig::new(&config, &req);
req_config.current_document = Some(document.get_id().to_string());

let resp = fastn_core::package::package_doc::process_ftd(
&mut req_config,
doc,
async fn f<'a>(
mut req_config: fastn_core::RequestConfig<'a>,
base_url: &str,
test: bool,
doc: &fastn_core::Document,
file_path: &str,
build_static_files: bool,
) -> (
fastn_core::RequestConfig<'a>,
fastn_core::Result<fastn_core::package::package_doc::FTDResult>,
) {
let resp = fastn_core::package::package_doc::process_ftd(
&mut req_config,
doc,
base_url,
build_static_files,
test,
file_path,
)
.await;

(req_config, resp)
}

let (req_config, resp) = f(
req_config,
base_url,
build_static_files,
test,
doc,
file_path.as_str(),
build_static_files,
)
.await;

let dependencies = req_config.dependencies_during_render;

match (resp, ignore_failed) {
(Ok(r), _) => {
let dependencies = req_config.dependencies_during_render;
if let Some(cache) = cache {
cache.documents.insert(
remove_extension(doc.id.as_str()),
Expand Down
8 changes: 4 additions & 4 deletions fastn-core/src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ fn handle_redirect(
/// path: /<file-name>/
///
#[tracing::instrument(skip_all)]
async fn serve_file<'a>(
config: &'a mut fastn_core::RequestConfig<'a>,
async fn serve_file<'m, 'o: 'm>(
config: &'m mut fastn_core::RequestConfig<'o>,
path: &camino::Utf8Path,
) -> fastn_core::http::Response {
if let Some(r) = handle_redirect(config.config, path) {
Expand Down Expand Up @@ -123,8 +123,8 @@ async fn serve_file<'a>(
}
}

async fn serve_cr_file<'a>(
req_config: &'a mut fastn_core::RequestConfig<'a>,
async fn serve_cr_file<'m, 'o: 'm>(
req_config: &'m mut fastn_core::RequestConfig<'o>,
path: &camino::Utf8Path,
cr_number: usize,
) -> fastn_core::http::Response {
Expand Down
2 changes: 1 addition & 1 deletion fastn-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct RequestConfig<'a> {
}

impl<'a> RequestConfig<'a> {
pub fn new(config: &Config, request: &fastn_core::http::Request) -> Self {
pub fn new(config: &'a Config, request: &'a fastn_core::http::Request) -> Self {
RequestConfig {
named_parameters: vec![],
extra_data: Default::default(),
Expand Down
10 changes: 5 additions & 5 deletions fastn-core/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn cached_parse(
pub async fn interpret_helper<'a>(
name: &str,
source: &str,
lib: &'a mut fastn_core::Library2022<'_>,
lib: &'a mut fastn_core::Library2022<'_, '_>,
base_url: &str,
download_assets: bool,
line_number: usize,
Expand Down Expand Up @@ -160,7 +160,7 @@ pub async fn resolve_import<'a>(

// source, foreign_variable, foreign_function
pub async fn resolve_import_2022<'a>(
lib: &'a mut fastn_core::Library2022<'_>,
lib: &'a mut fastn_core::Library2022<'_, '_>,
_state: &mut ftd::interpreter::InterpreterState,
module: &str,
caller_module: &str,
Expand Down Expand Up @@ -291,7 +291,7 @@ pub async fn resolve_import_2022<'a>(
pub async fn resolve_foreign_variable2022(
variable: &str,
doc_name: &str,
lib: &mut fastn_core::Library2022<'_>,
lib: &mut fastn_core::Library2022<'_, '_>,
base_url: &str,
download_assets: bool,
caller_module: &str,
Expand Down Expand Up @@ -353,7 +353,7 @@ pub async fn resolve_foreign_variable2022(
module: &str,
package: &fastn_core::Package,
files: &str,
lib: &mut fastn_core::Library2022<'_>,
lib: &mut fastn_core::Library2022<'_, '_>,
base_url: &str,
download_assets: bool, // true: in case of `fastn build`
) -> ftd::ftd2021::p1::Result<ftd::interpreter::Value> {
Expand Down Expand Up @@ -547,7 +547,7 @@ pub async fn resolve_foreign_variable2022(
}

async fn download(
lib: &mut fastn_core::Library2022<'_>,
lib: &mut fastn_core::Library2022<'_, '_>,
download_assets: bool,
package: &fastn_core::Package,
path: &str,
Expand Down
2 changes: 1 addition & 1 deletion fastn-core/src/library/fastn_dot_ftd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ pub(crate) async fn get2(lib: &fastn_core::Library2<'_>) -> String {
get(&lib).await
}

pub(crate) async fn get2022(lib: &fastn_core::Library2022<'_>) -> String {
pub(crate) async fn get2022(lib: &fastn_core::Library2022<'_, '_>) -> String {
let lib = fastn_core::Library {
config: lib.config.clone(),
markdown: lib.markdown.clone(),
Expand Down
10 changes: 5 additions & 5 deletions fastn-core/src/library2022/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ impl KeyValueData {
}

#[derive(Debug)]
pub struct Library2022<'a> {
pub config: &'a mut fastn_core::RequestConfig<'a>,
pub struct Library2022<'m, 'o: 'm> {
pub config: &'m mut fastn_core::RequestConfig<'o>,
/// If the current module being parsed is a markdown file, `.markdown` contains the name and
/// content of that file
pub markdown: Option<(String, String)>,
Expand All @@ -25,7 +25,7 @@ pub struct Library2022<'a> {
pub module_package_map: std::collections::BTreeMap<String, String>,
}

impl Library2022<'_> {
impl Library2022<'_, '_> {
pub async fn get_with_result(
&mut self,
name: &str,
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Library2022<'_> {

async fn get_for_package(
name: &str,
lib: &mut fastn_core::Library2022<'_>,
lib: &mut fastn_core::Library2022<'_, '_>,
current_processing_module: &str,
) -> Option<(String, String, usize)> {
let package = lib.get_current_package(current_processing_module).ok()?;
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Library2022<'_> {
async fn get_data_from_package(
name: &str,
package: &fastn_core::Package,
lib: &mut fastn_core::Library2022<'_>,
lib: &mut fastn_core::Library2022<'_, '_>,
) -> Option<(String, usize)> {
lib.push_package_under_process(name, package).await.ok()?;
let packages = lib.config.config.all_packages.borrow();
Expand Down
16 changes: 8 additions & 8 deletions fastn-core/src/package/package_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ impl From<FTDResult> for fastn_core::http::Response {
}

#[tracing::instrument(skip_all)]
pub(crate) async fn read_ftd<'a>(
config: &'a mut fastn_core::RequestConfig<'a>,
pub(crate) async fn read_ftd<'m, 'o: 'm>(
config: &'m mut fastn_core::RequestConfig<'o>,
main: &fastn_core::Document,
base_url: &str,
download_assets: bool,
Expand All @@ -385,8 +385,8 @@ pub(crate) async fn read_ftd<'a>(
}

#[tracing::instrument(name = "read_ftd_2022", skip_all)]
pub(crate) async fn read_ftd_2022<'a>(
config: &'a mut fastn_core::RequestConfig<'a>,
pub(crate) async fn read_ftd_2022<'m, 'o: 'm>(
config: &'m mut fastn_core::RequestConfig<'o>,
main: &fastn_core::Document,
base_url: &str,
download_assets: bool,
Expand Down Expand Up @@ -460,8 +460,8 @@ pub(crate) async fn read_ftd_2022<'a>(

#[allow(clippy::await_holding_refcell_ref)]
#[tracing::instrument(name = "read_ftd_2023", skip_all)]
pub(crate) async fn read_ftd_2023<'a>(
config: &'a mut fastn_core::RequestConfig<'a>,
pub(crate) async fn read_ftd_2023<'m, 'o: 'm>(
config: &'m mut fastn_core::RequestConfig<'o>,
main: &fastn_core::Document,
base_url: &str,
download_assets: bool,
Expand Down Expand Up @@ -541,8 +541,8 @@ pub(crate) async fn read_ftd_2023<'a>(
Ok(FTDResult::Html(file_content.into()))
}

pub(crate) async fn process_ftd<'a>(
config: &'a mut fastn_core::RequestConfig<'a>,
pub(crate) async fn process_ftd<'m, 'o: 'm>(
config: &'m mut fastn_core::RequestConfig<'o>,
main: &fastn_core::Document,
base_url: &str,
build_static_files: bool,
Expand Down
2 changes: 1 addition & 1 deletion fastn/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn fastn_core_commands(matches: &clap::ArgMatches) -> fastn_core::Result<(
return fastn_core::tutor::main("the-tutor".to_string()).await;
}

let mut config = fastn_core::Config::read(None, true, None).await?;
let mut config = fastn_core::Config::read(None, true).await?;
let package_name = config.package.name.clone();

if let Some(serve) = matches.subcommand_matches("serve") {
Expand Down
18 changes: 15 additions & 3 deletions foo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,30 @@ fn main() {
}

async fn outer_main() {
let mut i = P { x: 10, y: 20 };
let mut i = P { x: 10 };

yo(&mut i).await;
println!("Hello, world: {}", i.x);

let mut q = Q { p: &mut i };
bo(&mut q).await;

println!("Hello, world: {}", q.p.x);
}

struct P {
x: i32,
y: i32,
}

struct Q<'a> {
p: &'a mut P,
}

async fn yo(i: &mut P) {
i.x = 20;
println!("yo: {}", i.x);
}

async fn bo<'m, 'o: 'm>(i: &'m mut Q<'o>) {
i.p.x = 30;
println!("bo: {}", i.p.x);
}

0 comments on commit 222a608

Please sign in to comment.